Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;
import java.io.FileInputStream;

import java.util.HashMap;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class Main {
    public static HashMap xmltoHashMap222(String xmlFile, String xpath) {
        try {
            XPathFactory factory = XPathFactory.newInstance();
            XPath xPath = factory.newXPath();
            // XPathExpression xPathExpression =
            // xPath.compile("/history");
            File xmlDocument = new File(xmlFile);
            InputSource inputSource = new InputSource(new FileInputStream(xmlDocument));
            // String root = xPath.evaluate("/", inputSource);
            NodeList nodes = (NodeList) xPath.evaluate(xpath, inputSource, XPathConstants.NODESET);
            HashMap hashmap = new HashMap();
            for (int x = 0; x < nodes.getLength(); x++) {
                hashmap.put(nodes.item(x).getNodeName(), nodes.item(x).getTextContent());
            }
            return hashmap;
        } catch (Exception ex) {
            return null;
        }
    }
}