Here you can find the source of getNodeListAsArray(Node doc, String xpath)
public static Node[] getNodeListAsArray(Node doc, String xpath)
//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; } }); } } }