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.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;

import org.w3c.dom.NodeList;

public class Main {
    /**
     * Query xml result.
     * 
     * @param doc
     *            the doc
     * @param xpathExpression
     *            the xpath expression
     * @return the object
     */
    public static NodeList queryXMLResult(Document doc, String xpathExpression) {
        // Document doc = getDocument(result);
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        NodeList XmlResult = null;
        try {
            XPathExpression expr = xpath.compile(xpathExpression);
            XmlResult = evaluateXpath(doc, expr);
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }

        return XmlResult;
    }

    /**
     * Evaluate xpath.
     * 
     * @param doc
     *            the doc
     * @param expr
     *            the expr
     * @return the object
     */
    public static NodeList evaluateXpath(Document doc, XPathExpression expr) {
        NodeList XmlResult = null;
        try {
            XmlResult = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
        return XmlResult;
    }
}