Java XML Child Element Text getChildText(final Node node)

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

Description

get Child Text

License

Apache License

Declaration

public static String getChildText(final Node node) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.Node;

public class Main {
    public static String getChildText(final Node node) {
        if (node == null) {
            return null;
        } else {// w ww. j  ava2 s .  c o m
            final StringBuilder text = new StringBuilder();
            appendChildText(text, node);
            return text.toString();
        }
    }

    public static void appendChildText(final StringBuilder text, final Node node) {
        if (node != null) {
            for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
                final short type = child.getNodeType();
                if (type == Node.TEXT_NODE) {
                    final String childText = child.getNodeValue();
                    text.append(childText);
                } else if (type == Node.CDATA_SECTION_NODE) {
                    appendChildText(text, child);
                }
            }
        }

    }
}

Related

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