Java XML Element Children getSubelementBoolean(final Element element, final String element_name, final boolean default_value)

Here you can find the source of getSubelementBoolean(final Element element, final String element_name, final boolean default_value)

Description

Locate a sub-element tagged 'name', return its boolean value.

License

Open Source License

Parameter

Parameter Description
element Element where to start looking. May be null.
element_name Name of sub-element to locate.
default_value Default to use if tag's value is neither "true" nor "false".

Return

Returns true if sub-element was "true", false if it was "false", otherwise the default.

Declaration

public static final boolean getSubelementBoolean(final Element element, final String element_name,
        final boolean default_value) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/

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

public class Main {
    /** Locate a sub-element tagged 'name', return its boolean value.
     */*from w w  w.j ava  2 s.com*/
     *  Will only go one level down, not search the whole tree.
     *
     *  @param element Element where to start looking. May be null.
     *  @param element_name Name of sub-element to locate.
     *
     *  @return Returns <code>true</code> if sub-element was "true".
     *          If nothing was found, the default is <code>false</code>.
     */
    public static final boolean getSubelementBoolean(final Element element, final String element_name) {
        return getSubelementBoolean(element, element_name, false);
    }

    /** Locate a sub-element tagged 'name', return its boolean value.
     *
     *  Will only go one level down, not search the whole tree.
     *
     *  @param element Element where to start looking. May be null.
     *  @param element_name Name of sub-element to locate.
     *  @param default_value Default to use if tag's value is neither "true" nor "false".
     *
     *  @return Returns <code>true</code> if sub-element was "true",
     *          <code>false</code> if it was "false", otherwise the default.
     */
    public static final boolean getSubelementBoolean(final Element element, final String element_name,
            final boolean default_value) {
        final String s = getSubelementString(element, element_name);
        if (s.equalsIgnoreCase("true"))
            return true;
        if (s.equalsIgnoreCase("false"))
            return false;
        return default_value;
    }

    /** Locate a sub-element tagged 'name', return its value.
     *
     *  Will only go one level down, not search the whole tree.
     *
     *  @param element Element where to start looking. May be null.
     *  @param name Name of sub-element to locate.
     *
     *  @return Returns string that was found or empty string.
     */
    public static final String getSubelementString(final Element element, final String name) {
        if (element == null)
            return "";
        Node n = element.getFirstChild();
        n = findFirstElementNode(n, name);
        if (n != null) {
            Node text_node = n.getFirstChild();
            if (text_node == null)
                return "";
            return text_node.getNodeValue();
        }
        return "";
    }

    /** Locate a sub-element tagged 'name', return its value.
     *
     *  Will only go one level down, not search the whole tree.
     *
     *  @param element Element where to start looking. May be null.
     *  @param name Name of sub-element to locate.
     *  @param default_value Default value if not found
     *
     *  @return Returns string that was found or default_value.
     */
    public static final String getSubelementString(final Element element, final String name,
            final String default_value) {
        if (element == null)
            return default_value;
        Node n = element.getFirstChild();
        n = findFirstElementNode(n, name);
        if (n != null) {
            Node text_node = n.getFirstChild();
            if (text_node == null)
                return ""; // <name/>: Node is there, "" as value
            return text_node.getNodeValue();
        }
        return default_value;
    }

    /** Look for Element node if given name.
     *  <p>
     *  Checks the node and its siblings.
     *  Does not descent down the 'child' links.
     *  @param node Node where to start.
     *  @param name Name of the nodes to look for.
     *  @return Returns node or the next matching sibling or null.
     */
    public static final Element findFirstElementNode(Node node, final String name) {
        while (node != null) {
            if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name))
                return (Element) node;
            node = node.getNextSibling();
        }
        return null;
    }
}

Related

  1. getFirstSubElement(Element element)
  2. getImmediateSubElements(Element element, String name)
  3. getRelationUri(Element subject, Element object, String defaultBaseUri)
  4. getSubElement(Element ele, String tagName)
  5. getSubElement(Element element, String subTagName)
  6. getSubelementDouble(final Element element, final String element_name)
  7. getSubelementDouble(final Element element, final String name, final double default_value)
  8. getSubelementString(final Element element, final String name)
  9. getSubelementString(final Element element, final String name)