Java XML Child Element Text getChildElementText(final Element parent, final String childName, final String defaultText)

Here you can find the source of getChildElementText(final Element parent, final String childName, final String defaultText)

Description

get Child Element Text

License

Open Source License

Parameter

Parameter Description
parent the parent XML element
childName the child node name
defaultText the default text if child element does not exist

Return

the child XML element text with specified node name or default value if does not exist

Declaration

public static String getChildElementText(final Element parent, final String childName,
        final String defaultText) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**/*from w  w  w.  j  a  v  a  2 s.  co m*/
     * @param parent the parent XML element
     * @param childName the child node name
     * @return the child XML element text with specified node name or <code>null</code> if does not exist
     */
    public static String getChildElementText(final Element parent, final String childName) {

        Element child = getChildElement(parent, childName);
        return child == null ? null : child.getTextContent();
    }

    /**
     * @param parent the parent XML element
     * @param childName the child node name
     * @param defaultText the default text if child element does not exist
     * @return the child XML element text with specified node name or default value if does not exist
     */
    public static String getChildElementText(final Element parent, final String childName,
            final String defaultText) {

        Element child = getChildElement(parent, childName);
        return child == null ? defaultText : child.getTextContent();
    }

    /**
     * @param parent the parent XML element
     * @param childName the child node name
     * @return the child XML element with specified node name or <code>null</code> if does not exist
     */
    public static Element getChildElement(final Element parent, final String childName) {

        Element child = null;
        if (parent != null) {
            NodeList children = parent.getElementsByTagName(childName);
            if (children.getLength() > 0) {
                child = (Element) children.item(0);
            }
        }
        return child;
    }
}

Related

  1. GetChildElementText(Element element, String name, String defaultValue)
  2. getChildElementText(Element elm, String name, String defaultValue)
  3. getChildElementText(Element elm, String name, String defaultValue)
  4. getChildElementText(Element parentElement, String tagName)
  5. getChildElementText(final Element elParent, final String childTag)
  6. getChildElementText(Node node, String childName)
  7. getChildElementTextArr(Element parent, String name)
  8. getChildElementTextValue(Node parent, String name)
  9. getChildElementValue(Element p_rootElement, String p_elementName)