Java XML Child Element Text getChildTextAsBooleanObj(Element parent, String childName, Boolean defValue)

Here you can find the source of getChildTextAsBooleanObj(Element parent, String childName, Boolean defValue)

Description

Get the text content of a given child name as a java.lang.Boolean value.

License

Open Source License

Parameter

Parameter Description
parent Parent Element
childName Tag name of the child element whose text content to be parsed.
defValue Default Boolean value if a valid Boolean text not found.

Return

Returns true if a child Element is found, whose text content is 'true' Returns false if a child Element is found, whose text content is 'false' Returns defValue otherwise.

Declaration

public static final Boolean getChildTextAsBooleanObj(Element parent, String childName, Boolean defValue) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**//from w ww  . ja v  a2  s  .  com
     * Get the text content of a given child name as a java.lang.Boolean value.
     * 
     * @param parent
     *            Parent Element
     * @param childName
     *            Tag name of the child element whose text content to be parsed.
     * @param defValue
     *            Default Boolean value if a valid Boolean text not found.
     * 
     * @return Returns true if a child Element is found, whose text content is 'true' Returns false if a child Element
     *         is found, whose text content is 'false' Returns defValue otherwise.
     */
    public static final Boolean getChildTextAsBooleanObj(Element parent, String childName, Boolean defValue) {
        try {
            String value = getChildText(parent, childName);

            if (value != null) {
                return Boolean.valueOf(value);
            }
        } catch (Exception ex) {
        }

        return defValue;
    }

    /**
     * Get the text content of a given child name.
     * 
     * @param parent
     *            Parent Element
     * @param childName
     *            Tag name of the child element
     * 
     * @return Text content of a child element with the given tag name with the parent element. null if no child element
     *         is found. null or empty string if child element is found, but does not have text content.
     */
    public static final String getChildText(Element parent, String childName) {
        Element childElement = getFirstLevelChildElementByTagName(parent, childName);

        if (childElement != null) {
            return getText(childElement);
        }

        return null;
    }

    /**
     * Get the immediate child element with a given tag name. If multiple child elements exist with the same tag name,
     * the first child element is returned.
     * 
     * @param parent
     *            Parent Element
     * @param elementName
     *            Tag name of the child elements to be returned.
     * 
     * @return First child element which has the given tag name.
     */
    public static final Element getFirstLevelChildElementByTagName(Element parent, String elementName) {
        Node childNode = parent.getFirstChild();
        while (childNode != null) {
            if ((childNode.getNodeType() == Node.ELEMENT_NODE)
                    && ((Element) childNode).getLocalName().equals(elementName)) {
                return (Element) childNode;
            }
            childNode = childNode.getNextSibling();
        }
        return null;
    }

    /**
     * Get Text Content of the given element.
     * 
     * @param elem
     *            Element
     * 
     * @return Trimmed text content of the element.
     */
    public static final String getText(Element elem) {
        if (elem != null) {
            NodeList childNodes = elem.getChildNodes();
            for (int i = 0; i < childNodes.getLength(); i++) {
                if (childNodes.item(i).getNodeType() == Node.TEXT_NODE) {
                    return trim(childNodes.item(i).getNodeValue());
                }
            }
        }

        return null;
    }

    private static String trim(String input) {
        if (input == null) {
            return input;
        }

        return input.trim();

    }
}

Related

  1. getChildText(Node node)
  2. getChildText(Node node)
  3. getChildText(Node node)
  4. getChildText(Node parent, String childName)
  5. getChildTextAsBoolean(Element parent, String childName, boolean defValue)
  6. getChildTextByName(Element parent, String name)
  7. getChildTextByTagName(Element e, String tagName)
  8. getChildTextContent(Element elemenet, String childElemenetName)
  9. getChildTextContent(Element element, String childTagName)