Java XML Child Node Get getChildNodeText(Element element, String nodeTagName)

Here you can find the source of getChildNodeText(Element element, String nodeTagName)

Description

Returns the text of the child element identified by nodeTagName.

License

CDDL license

Parameter

Parameter Description
element This is the element where the child element may be found.
nodeTagName This is the name of the child element.

Exception

Parameter Description
Exception if an error is encountered.

Return

text of the child element.

Declaration

public static String getChildNodeText(Element element, String nodeTagName) throws Exception 

Method Source Code


//package com.java2s;
/*//from   www  .jav a2s .  c  om
 * BEGIN_HEADER - DO NOT EDIT
 *
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the "License").  You may not use this file except
 * in compliance with the License.
 *
 * You can obtain a copy of the license at
 * https://open-esb.dev.java.net/public/CDDLv1.0.html.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * HEADER in each file and include the License file at
 * https://open-esb.dev.java.net/public/CDDLv1.0.html.
 * If applicable add the following below this CDDL HEADER,
 * with the fields enclosed by brackets "[]" replaced with
 * your own identifying information: Portions Copyright
 * [year] [name of copyright owner]
 */

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /** 
     * Returns the text of the child element identified by nodeTagName.
     *
     * @param element This is the element where the child element may be found.
     * @param nodeTagName This is the name of the child element.
     * @return text of the child element.
     * @throws Exception if an error is encountered.
     */
    public static String getChildNodeText(Element element, String nodeTagName) throws Exception {

        NodeList elements = element.getElementsByTagName(nodeTagName);

        for (int j = 0; j < elements.getLength(); j++) {
            Node node = (Node) elements.item(j);
            NodeList elementsChild = node.getChildNodes();
            for (int k = 0; k < elementsChild.getLength(); k++) {
                Node node2 = (Node) elementsChild.item(k);
                if (node2.getNodeType() == Node.TEXT_NODE) {
                    return node2.getNodeValue();
                }
            }
        }
        return null;
    }
}

Related

  1. getChildNodesByName(Node element, String name, boolean caseSensitive)
  2. getChildNodesByName(Node element, String name, boolean caseSensitive)
  3. getChildNodesByName(Node node, String name)
  4. getChildNodesByTagName(final Element element, final String tagName)
  5. getChildNodesWithName(Node node, String name)
  6. getChildNodeText(Node root, String childName)
  7. getChildNodeTextContent(Node parentNode, String childElementName)
  8. getChildNodeTextContents(Node node, String name)
  9. getChildNodeValue(Element node, String tagName)