Here you can find the source of getTextNodes(Node contextNode, String xPath)
public static String[] getTextNodes(Node contextNode, String xPath) throws TransformerException
//package com.java2s; /*//from w w w . ja v a2 s . c o m * Copyright (C) 2006-2016 Talend Inc. - www.talend.com * * This source code is available under agreement available at * %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt * * You should have received a copy of the agreement along with this program; if not, write to Talend SA 9 rue Pages * 92150 Suresnes, France */ import java.util.Collections; import java.util.Iterator; import javax.xml.namespace.NamespaceContext; import javax.xml.transform.TransformerException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static String[] getTextNodes(Node contextNode, String xPath) throws TransformerException { return getTextNodes(contextNode, xPath, contextNode); } private static String[] getTextNodes(Node contextNode, String xPath, final Node namespaceNode) throws TransformerException { String[] results; // test for hard-coded values if (xPath.startsWith("\"") && xPath.endsWith("\"")) { //$NON-NLS-1$ //$NON-NLS-2$ return new String[] { xPath.substring(1, xPath.length() - 1) }; } // test for incomplete path (elements missing /text()) if (!xPath.matches(".*@[^/\\]]+")) { // attribute if (!xPath.endsWith(")")) { // function xPath += "/text()"; } } try { XPath path = XPathFactory.newInstance().newXPath(); path.setNamespaceContext(new NamespaceContext() { @Override public String getNamespaceURI(String s) { return namespaceNode.getNamespaceURI(); } @Override public String getPrefix(String s) { return namespaceNode.getPrefix(); } @Override public Iterator getPrefixes(String s) { return Collections.singleton(namespaceNode.getPrefix()).iterator(); } }); NodeList xo = (NodeList) path.evaluate(xPath, contextNode, XPathConstants.NODESET); results = new String[xo.getLength()]; for (int i = 0; i < xo.getLength(); i++) { results[i] = xo.item(i).getTextContent(); } } catch (Exception e) { String err = "Unable to get the text node(s) of " + xPath + ": " + e.getClass().getName() + ": " + e.getLocalizedMessage(); throw new TransformerException(err); } return results; } }