Java XML Child Element Text getChildElementTextValue(Node parent, String name)

Here you can find the source of getChildElementTextValue(Node parent, String name)

Description

Returns the text value of a child element with the given name, of the given parent element, or null if the child does not exist or does not have a child text node

License

Educational Community License

Parameter

Parameter Description
parent parent element
name name of child element

Return

the text value of a child element with the given name, of the given parent element, or null if the child does not exist or does not have a child text node

Declaration

public static String getChildElementTextValue(Node parent, String name) 

Method Source Code

//package com.java2s;
/**//w  w w .j  a  va  2  s  .  com
 * Copyright 2005-2014 The Kuali Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl2.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

public class Main {
    /**
     * Returns the text value of a child element with the given name, of the given parent element,
     * or null if the child does not exist or does not have a child text node
     * @param parent parent element
     * @param name name of child element
     * @return the text value of a child element with the given name, of the given parent element,
     * or null if the child does not exist or does not have a child text node
     */
    public static String getChildElementTextValue(Node parent, String name) {
        Element child = getChildElement(parent, name);
        if (child == null) {
            return null;
        }
        Node textNode = child.getFirstChild();
        if (textNode == null) {
            return null;
        }
        return textNode.getNodeValue();
    }

    /**
     * Returns a node child with the specified tag name of the specified parent node,
     * or null if no such child node is found.
     * @param parent the parent node
     * @param name the name of the child node
     * @return child node if found, null otherwise
     */
    public static Element getChildElement(Node parent, String name) {
        NodeList childList = parent.getChildNodes();
        for (int i = 0; i < childList.getLength(); i++) {
            Node node = childList.item(i);
            // we must test against NodeName, not just LocalName
            // LocalName seems to be null - I am guessing this is because
            // the DocumentBuilderFactory is not "namespace aware"
            // although I would have expected LocalName to default to
            // NodeName
            if (node.getNodeType() == Node.ELEMENT_NODE
                    && (name.equals(node.getLocalName()) || name
                            .equals(node.getNodeName()))) {
                return (Element) node;
            }
        }
        return null;
    }
}

Related

  1. getChildElementText(Element parentElement, String tagName)
  2. getChildElementText(final Element elParent, final String childTag)
  3. getChildElementText(final Element parent, final String childName, final String defaultText)
  4. getChildElementText(Node node, String childName)
  5. getChildElementTextArr(Element parent, String name)
  6. getChildElementValue(Element p_rootElement, String p_elementName)
  7. getChildElementValue(Element parent, String name)
  8. getChildElementValue(Element parentElm, String elementName, String defaultValue)
  9. getChildElementValueByTagName(Element ele, String childEleName)