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 getStringValue(final String xpathExpression, final NamespaceContext nsContext,
            final Node node) throws XPathExpressionException {

        return (String) getValue(xpathExpression, node, XPathConstants.STRING, nsContext);
    }

    private static Object getValue(final String xpathExpression, final Node node, final QName type,
            final NamespaceContext nsContext) throws XPathExpressionException {

        final XPathFactory factory = XPathFactory.newInstance();
        final XPath xpath = factory.newXPath();
        xpath.setNamespaceContext(nsContext);
        return xpath.evaluate(xpathExpression, node, type);
    }
}