Java XML Child Element Text getChildTextAsBoolean(Element parent, String childName, boolean defValue)

Here you can find the source of getChildTextAsBoolean(Element parent, String childName, boolean defValue)

Description

get Child Text As Boolean

License

Open Source License

Declaration

public static final boolean getChildTextAsBoolean(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 {
    public static final boolean getChildTextAsBoolean(Element parent, String childName, boolean defValue) {
        try {/*ww w . j a  v a 2s  .c o m*/
            String value = getChildText(parent, childName);

            if (value != null) {
                if (value.trim().equals("1")) {
                    return true;
                }
                if (value.trim().equals("0")) {
                    return false;
                }
                return Boolean.valueOf(value).booleanValue();
            }
        } catch (Exception ex) {
        }

        return defValue;
    }

    public static final String getChildText(Element parent, String childName) {
        Element childElement = getFirstLevelChildElementByTagName(parent, childName);

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

        return null;
    }

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

        return input.trim();

        // int len = input.length();
        //
        // int index = 0;
        //
        // for(index = 0;index < len;index++)
        // {
        // if(!Character.isWhitespace(input.charAt(index)))
        // {
        // break;
        // }
        // }
        //
        // return (index == 0) ? input : input.substring(index);
    }

    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;
    }

    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;
    }
}

Related

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