Java XML Element Check isTrue(Element el, String tagName, boolean defaultResult)

Here you can find the source of isTrue(Element el, String tagName, boolean defaultResult)

Description

is True

License

Open Source License

Declaration

public static boolean isTrue(Element el, String tagName,
            boolean defaultResult) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static boolean isTrue(Element el, String tagName,
            boolean defaultResult) {
        Element booleanElement = getElement(el, tagName);
        if (booleanElement == null) {
            return defaultResult;
        } else if (isTrueText(getNestedText(booleanElement))) {
            return true;
        }/*from   w ww .j  a  v  a 2s.c  o m*/
        return false;
    }

    /**
     * returns the first element with the given name, null if none exists.
     */
    public static Element getElement(Element config, String elementName) {
        NodeList children = config.getChildNodes();
        for (int counter = 0; counter < children.getLength(); counter++) {
            if (children.item(counter) instanceof Element) {
                Element el = (Element) children.item(counter);
                if (el.getTagName().equals(elementName)) {
                    return el;
                }
            }
        }
        return null;
    }

    public static boolean isTrueText(String nestedText) {
        if (nestedText.toUpperCase().equals("TRUE")) {
            return true;
        }
        return false;
    }

    /** returns the nested text in the tag * */
    public static String getNestedText(Element config) {
        // logger.debug("The element name in sod util is "+config.getTagName());
        String rtnValue = null;
        NodeList children = config.getChildNodes();
        Node node;
        // logger.debug("The length of the children is "+children.getLength());
        for (int i = 0; i < children.getLength(); i++) {
            node = children.item(i);
            if (node instanceof Text) {
                // logger.debug("In sodUtil textnode value is
                // "+node.getNodeValue());
                rtnValue = node.getNodeValue();
                // break;
            } else if (node instanceof Element) {
                // logger.debug("in sod util tag name is
                // "+((Element)node).getTagName());
                rtnValue = getNestedText((Element) node);
                break;
            }
        }
        return rtnValue;
    }
}

Related

  1. isSetPrefixBeforeStartElement(XMLStreamWriter writer)
  2. isStartElement(final XMLStreamReader reader)
  3. isStartElement(XMLStreamReader xmlRdr, String tagName)
  4. isTableNodes(Element e)
  5. isTextOnly(final Element element)
  6. isUIParameter(Element ele)
  7. isUnder5_6(Element root)
  8. isXmlElementChoice(Field f, String xmlElementName)
  9. isXmlElementRequired(Field f)