Java XPath Get getNodeListAsArray(Node doc, String xpath)

Here you can find the source of getNodeListAsArray(Node doc, String xpath)

Description

get Node List As Array

License

Open Source License

Declaration

public static Node[] getNodeListAsArray(Node doc, String xpath) 

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 {
    public static Node[] getNodeListAsArray(Node doc, String xpath) {
        try {/*  w  w w  .j  a va2  s  .c  o  m*/
            NodeList list = XPathAPI_selectNodeList(doc, xpath);
            int length = list.getLength();
            if (length > 0) {
                Node[] array = new Node[length];
                for (int i = 0; i < length; i++) {
                    array[i] = list.item(i);
                }
                return array;
            }
            return null;
        } catch (TransformerException ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 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. getNodeList(InputStream stream, String pattern)
  2. getNodeList(Node node, String path)
  3. getNodeList(Node node, String xpath)
  4. getNodeList(Node xmlNode, String xpathString)
  5. getNodeList(Reader reader, String expression)
  6. getNodeListAttValAsStringCol(final String xPath, final Node node, final String attrName)
  7. getNodes(Node node, String expStr)
  8. getNodesByPath(String path, Element localElement, Document doc)
  9. getNodesByXPath(Document doc, XPathExpression expr)