Java XPath Select selectNodeText(Node node, String expression)

Here you can find the source of selectNodeText(Node node, String expression)

Description

select Node Text

License

Open Source License

Declaration

public static String selectNodeText(Node node, String expression) throws XPathExpressionException 

Method Source Code


//package com.java2s;
/*/* ww  w  .j  a  v a 2 s  .c  o m*/
 * Copyright (c) 2004-2012 The YAWL Foundation. All rights reserved.
 * The YAWL Foundation is a collaboration of individuals and
 * organisations who are committed to improving workflow technology.
 *
 * This file is part of YAWL. YAWL is free software: you can
 * redistribute it and/or modify it under the terms of the GNU Lesser
 * General Public License as published by the Free Software Foundation.
 *
 * YAWL is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
 * Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with YAWL. If not, see <http://www.gnu.org/licenses/>.
 */

import org.w3c.dom.*;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

public class Main {
    public static String selectNodeText(Node node, String expression) throws XPathExpressionException {
        XPathFactory factory = XPathFactory.newInstance();
        XPath path = factory.newXPath();
        Object result = path.evaluate(expression, node, XPathConstants.NODE);
        return getNodeText((Node) result);
    }

    /**
     * Extracts to text from the supplied node and it's children.
     *
     * @param node to extract text from.
     * @return String representation of the node text.
     */
    public static String getNodeText(Node node) {

        if (node == null || !node.hasChildNodes())
            return "";
        StringBuilder result = new StringBuilder();

        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            Node subnode = list.item(i);
            if (subnode.getNodeType() == Node.TEXT_NODE) {
                result.append(subnode.getNodeValue());
            } else if (subnode.getNodeType() == Node.CDATA_SECTION_NODE) {
                result.append(subnode.getNodeValue());
            } else if (subnode.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
                // Recurse into the subtree for text
                // (and ignore comments)
                result.append(getNodeText(subnode));
            }
        }
        return result.toString();
    }
}

Related

  1. selectNodes(String express, Object source)
  2. selectNodes(String xPath, Node target)
  3. selectNodes(String xpath, Object node)
  4. selectNodes(String xpath, Object node)
  5. selectNodesViaXPath(XPath xPath, Node startingNode, String xPathExpression)
  6. selectSingleElement(Element element, String xpathExpression)
  7. selectSingleNode(final Node node, final String xPath)
  8. selectSingleNode(final Node sourceNode, final String xPathExpression)
  9. selectSingleNode(Node node, String xpath, NamespaceContext context)