Java XPath Get getStrFromNode(Node xpathnode)

Here you can find the source of getStrFromNode(Node xpathnode)

Description

Method getStrFromNode

License

Apache License

Parameter

Parameter Description
xpathnode a parameter

Return

the string for the node.

Declaration

public static String getStrFromNode(Node xpathnode) 

Method Source Code

//package com.java2s;
/* NOTICE: This file has been changed by Plutext Pty Ltd for use in docx4j.
 * The package name has been changed; there may also be other changes.
 * //from ww  w.  j  av  a2 s .com
 * This notice is included to meet the condition in clause 4(b) of the License. 
 */

import org.w3c.dom.Node;

import org.w3c.dom.Text;

public class Main {
    /**
     * Method getStrFromNode
     *
     * @param xpathnode
     * @return the string for the node.
     */
    public static String getStrFromNode(Node xpathnode) {
        if (xpathnode.getNodeType() == Node.TEXT_NODE) {
            // we iterate over all siblings of the context node because eventually,
            // the text is "polluted" with pi's or comments
            StringBuilder sb = new StringBuilder();

            for (Node currentSibling = xpathnode.getParentNode()
                    .getFirstChild(); currentSibling != null; currentSibling = currentSibling.getNextSibling()) {
                if (currentSibling.getNodeType() == Node.TEXT_NODE) {
                    sb.append(((Text) currentSibling).getData());
                }
            }

            return sb.toString();
        } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) {
            return xpathnode.getNodeValue();
        } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
            return xpathnode.getNodeValue();
        }

        return null;
    }
}

Related

  1. getResultXpathstring(String expr, InputSource inputSource)
  2. getScrProperties(String componentName)
  3. getSearchHandlerNode(final File solrconfig)
  4. getSharedXPath()
  5. getSpeficValueFromNode(Node n, String xpathExpr)
  6. getStrFromNode(Node xpathnode)
  7. getString(final String xPath, final Object item)
  8. getString(Node node, XPathExpression expr)
  9. getStringByXPath(String xml, String xpathStr)