Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

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 XPATH_FACTORY = XPathFactory.newInstance();

    public static Element getElementByXpath(Element sourceRoot, String xpathExpress)
            throws XPathExpressionException {
        XPath xPath = XPATH_FACTORY.newXPath();
        NodeList nodes = (NodeList) xPath.evaluate(xpathExpress, sourceRoot, XPathConstants.NODESET);
        if (nodes.getLength() > 0)
            return (Element) nodes.item(0);
        return null;
    }
}