Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    protected static XPathFactory xpath = XPathFactory.newInstance();

    public static Element querySingle(Element element, String query) {
        try {
            XPathExpression expr = xpath.newXPath().compile(query);
            NodeList list = (NodeList) expr.evaluate(element, XPathConstants.NODESET);

            if (list.getLength() > 0)
                return (Element) list.item(0);

        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }

        return null;
    }
}