Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    private static final XPathFactory factory = XPathFactory.newInstance();

    public static Long extractValueId(Element element, String xpath) throws XPathExpressionException {
        XPath xpathObj = factory.newXPath();
        NodeList nodeList = (NodeList) xpathObj.compile(xpath).evaluate(element, XPathConstants.NODESET);

        for (int i = 0; i < nodeList.getLength(); i++) {
            String val = nodeList.item(i).getFirstChild().getNodeValue();
            return Long.parseLong(val);
        }

        return null;
    }
}