Java XPath Select XPathAPI_selectNodeList(Document doc, String xpath, Node namespaceNode)

Here you can find the source of XPathAPI_selectNodeList(Document doc, String xpath, Node namespaceNode)

Description

X Path APselect Node List

License

Open Source License

Declaration

private static NodeList XPathAPI_selectNodeList(Document doc,
            String xpath, Node namespaceNode) throws TransformerException 

Method Source Code

//package com.java2s;

import java.util.Iterator;

import javax.xml.transform.TransformerException;

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.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**/*  ww w.  j  av  a 2  s .  co m*/
     * Compatibility.
     *
     * @param node
     * @param xpath
     * @return
     * @throws TransformerException
     */
    private static NodeList XPathAPI_selectNodeList(Node node, String xpath)
            throws TransformerException {
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        setNamespace(xPath, xpath);
        try {
            XPathExpression xPathExpression = xPath.compile(xpath);
            return (NodeList) xPathExpression.evaluate(node,
                    XPathConstants.NODESET);
        } catch (XPathExpressionException e) {
            throw new TransformerException(
                    "Exception in XPathAPI_selectNodeList: " + xpath, e);
        }
    }

    private static NodeList XPathAPI_selectNodeList(Document doc,
            String xpath, Node namespaceNode) throws TransformerException {
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        setNamespace(xPath, xpath);
        try {
            XPathExpression xPathExpression = xPath.compile(xpath);
            return (NodeList) xPathExpression.evaluate(doc,
                    XPathConstants.NODESET);
        } catch (XPathExpressionException e) {
            throw new TransformerException(
                    "Exception in XPathAPI_selectNodeList: " + xpath, e);
        }
    }

    private static NodeList XPathAPI_selectNodeList(Node context,
            String xpath, Node namespaceNode) throws TransformerException {
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        setNamespace(xPath, xpath);
        try {
            XPathExpression xPathExpression = xPath.compile(xpath);
            return (NodeList) xPathExpression.evaluate(context,
                    XPathConstants.NODESET);
        } catch (XPathExpressionException e) {
            throw new TransformerException(
                    "Exception in XPathAPI_selectNodeList: " + xpath, e);
        }
    }

    private static void setNamespace(XPath xpath, String string) {
        if (string.contains("special/icu:")) {
            xpath.setNamespaceContext(new javax.xml.namespace.NamespaceContext() {
                public String getNamespaceURI(String prefix) {
                    if (prefix.equals("icu")) {
                        return "http://www.icu-project.org";
                    }
                    return null;
                }

                public String getPrefix(String namespaceURI) {
                    return null;
                }

                public Iterator<Object> getPrefixes(String namespaceURI) {
                    return null;
                }
            });
        }
    }
}

Related

  1. selectSingleNode(Node node, String xpath, NamespaceContext context)
  2. selectString(String xpath, Object node)
  3. selectStrings(String xpath, Object node)
  4. selectXPathString(final String xPath, final Node inNode, final String nsuri, final String pre)
  5. xmlSelectNodes(Node node, String xpathExpression)
  6. xpathNode(String expr, Document doc)
  7. xpathNodeList(String expr, Document doc)
  8. xpathOrNull(Document doc, String xp)
  9. xpathString(String expr, Document doc)