Java XPath Get getTextNodes(Node contextNode, String xPath)

Here you can find the source of getTextNodes(Node contextNode, String xPath)

Description

get Text Nodes

License

Open Source License

Declaration

public static String[] getTextNodes(Node contextNode, String xPath) throws TransformerException 

Method Source Code

//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;

    }
}

Related

  1. getStringValue(Node node, XPathExpression expression)
  2. getStringValue(Object doc, XPathExpression path)
  3. getStringValue(String targetDoc, String xpathExp, String encoding)
  4. getStringValueByXPath(Node node, String xPath)
  5. getText(String xPathExpression, Node node)
  6. getValidXpath(String xPath, NamespaceContext context)
  7. getValue(final String expression, final String xml)
  8. getValueByPath(Node node, String path)
  9. getValueByXpath(Node doc, String xquery)