Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Node;

public class Main {
    public static String xpathString(Node node, String expression) throws XPathExpressionException {
        return (String) evaluateXpath(expression, node, XPathConstants.STRING, null);
    }

    public static Object evaluateXpath(String expression, Object node, QName returnType, NamespaceContext nsContext)
            throws XPathExpressionException {
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        if (nsContext != null) {
            xpath.setNamespaceContext(nsContext);
        }

        return xpath.evaluate(expression, node, returnType);
    }
}