Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import javax.xml.namespace.QName;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;

import org.w3c.dom.NodeList;

public class Main {
    public static NodeList findNodes(Document doc, String xpath) {
        return (NodeList) xpathFind(doc, xpath, XPathConstants.NODESET);
    }

    public static Object xpathFind(Document doc, String xpath, QName returnType) {
        try {
            XPathFactory xpathFactory = XPathFactory.newInstance();
            XPath xpathToolkit = xpathFactory.newXPath();
            XPathExpression xpathExpression = xpathToolkit.compile(xpath);
            return xpathExpression.evaluate(doc, returnType);
        } catch (Exception e) {
            throw new RuntimeException("Error finding nodes for <" + xpath + ">", e);
        }
    }
}