Java XML Child Element Text getChildText(Node node)

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

Description

We get a object that connected text node of the child node and the CDATA section as character string.

License

Open Source License

Parameter

Parameter Description
node a parameter

Declaration

public static String getChildText(Node node) 

Method Source Code

//package com.java2s;
/* infoScoop OpenSource//from   w w  w .  j  a va  2 s. com
 * Copyright (C) 2010 Beacon IT Inc.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.
 * 
 * This program 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 this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0-standalone.html>.
 */

import org.w3c.dom.Node;

public class Main {
    /**
     * We get a object that connected text node of the child node and the CDATA section as character string.
     * @param node
     * @return
     */
    public static String getChildText(Node node) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < node.getChildNodes().getLength(); i++) {
            Node n = node.getChildNodes().item(i);
            if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE) {
                buf.append(n.getNodeValue());
            }
        }
        return buf.toString();
    }
}

Related

  1. getChildText(final Element element, final String tagName)
  2. getChildText(final Element parentElem, final String childName)
  3. getChildText(final Node node)
  4. getChildText(final Node node)
  5. getChildText(Node node)
  6. getChildText(Node node)
  7. getChildText(Node parent, String childName)
  8. getChildTextAsBoolean(Element parent, String childName, boolean defValue)
  9. getChildTextAsBooleanObj(Element parent, String childName, Boolean defValue)