Java XML Child Element Text getChildText(final Node node)

Here you can find the source of getChildText(final Node node)

Description

Returns the concatenated child text of the specified node.

License

Open Source License

Parameter

Parameter Description
node The node to look at.

Declaration

public static String getChildText(final Node node) 

Method Source Code

//package com.java2s;
/*/*from   w w  w  . jav  a 2s .  c om*/
 * JBoss, Home of Professional Open Source.
 *
 * See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
 *
 * See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
 */

import org.w3c.dom.Node;

public class Main {
    /**
     * Returns the concatenated child text of the specified node. This method only looks at the immediate children of type
     * <code>Node.TEXT_NODE</code> or the children of any child node that is of type <code>Node.CDATA_SECTION_NODE</code> for the
     * concatenation. This method was copied from the org.apache.xerces.util.DOMUtil class.
     * 
     * @param node The node to look at.
     */
    public static String getChildText(final Node node) {

        // is there anything to do?
        if (node == null) {
            return null;
        }

        // concatenate children text
        StringBuffer str = new StringBuffer();
        Node child = node.getFirstChild();
        while (child != null) {
            short type = child.getNodeType();
            if (type == Node.TEXT_NODE) {
                str.append(child.getNodeValue());
            } else if (type == Node.CDATA_SECTION_NODE) {
                str.append(getChildText(child));
            }
            child = child.getNextSibling();
        }

        // return text value
        return str.toString();

    }
}

Related

  1. getChildText(Element root, String childName)
  2. getChildText(Element tag, String childTagName)
  3. getChildText(final Element element, final String tagName)
  4. getChildText(final Element element, final String tagName)
  5. getChildText(final Element parentElem, final String childName)
  6. getChildText(final Node node)
  7. getChildText(Node node)
  8. getChildText(Node node)
  9. getChildText(Node node)