Java XML Element Children getSubelementDouble(final Element element, final String element_name)

Here you can find the source of getSubelementDouble(final Element element, final String element_name)

Description

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

License

Open Source License

Parameter

Parameter Description
element Element where to start looking.
element_name Name of sub-element to locate.

Exception

Parameter Description
Exception when nothing found

Return

Returns number found in the sub-element.

Declaration

public static final double getSubelementDouble(final Element element, final String element_name)
        throws Exception 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 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
 *
 * The scan engine idea is based on the "ScanEngine" developed
 * by the Software Services Group (SSG),  Advanced Photon Source,
 * Argonne National Laboratory,//from  w  w w . j  a v a 2  s.co m
 * Copyright (c) 2011 , UChicago Argonne, LLC.
 *
 * This implementation, however, contains no SSG "ScanEngine" source code
 * and is not endorsed by the SSG authors.
 ******************************************************************************/

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

public class Main {
    /** Locate a sub-element tagged 'name', return its double value.
     *
     *  <p>Will only go one level down, not search the whole tree.
     *
     *  @param element Element where to start looking.
     *  @param element_name Name of sub-element to locate.
     *
     *  @return Returns number found in the sub-element.
     *  @throws Exception when nothing found
     */
    public static final double getSubelementDouble(final Element element, final String element_name)
            throws Exception {
        final String s = getSubelementString(element, element_name);
        try {
            return Double.parseDouble(s);
        } catch (NumberFormatException ex) {
            throw new Exception("Missing numeric value for '" + element_name + "'");
        }
    }

    /** Locate a sub-element tagged 'name', return its double value.
     *
     *  <p>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 Result in case sub-element isn't found.
     *
     *  @return Returns number found in the sub-element.
     *  @throws Exception when nothing found
     */
    public static final double getSubelementDouble(final Element element, final String element_name,
            final double default_value) throws Exception {
        final String s = getSubelementString(element, element_name, "");
        if (s.length() < 1)
            return default_value;
        try {
            return Double.parseDouble(s);
        } catch (NumberFormatException ex) {
            throw new Exception("Missing numeric value for '" + element_name + "'");
        }
    }

    /** Locate a sub-element tagged 'name', return its value.
     *
     *  <p>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)
            return default_value;
        // <name> has been found.
        // If it's an empty node, <name/>, that's treated as a value of "".
        // If the node wasn't there at all, the default would have been returned.
        Node text_node = n.getFirstChild();
        if (text_node == null)
            return "";
        return text_node.getNodeValue();
    }

    /** Locate a sub-element tagged 'name', return its value.
     *
     *  <p>Will only go one level down, not search the whole tree.
     *
     *  @param element Element where to start looking.
     *  @param name Name of sub-element to locate.
     *
     *  @return Returns string that was found
     *  @throws Exception when nothing found
     */
    public static final String getSubelementString(final Element element, final String name) throws Exception {
        if (element == null)
            throw new Exception("Missing '" + name + "'");
        Node n = element.getFirstChild();
        n = findFirstElementNode(n, name);
        if (n == null)
            throw new Exception("Missing '" + name + "'");
        // <name>...</name> has been found, but it might be empty: <name/>
        final Node text_node = n.getFirstChild();
        if (text_node == null)
            return "";
        return text_node.getNodeValue();
    }

    /** Look for Element node of 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. getImmediateSubElements(Element element, String name)
  2. getRelationUri(Element subject, Element object, String defaultBaseUri)
  3. getSubElement(Element ele, String tagName)
  4. getSubElement(Element element, String subTagName)
  5. getSubelementBoolean(final Element element, final String element_name, final boolean default_value)
  6. getSubelementDouble(final Element element, final String name, final double default_value)
  7. getSubelementString(final Element element, final String name)
  8. getSubelementString(final Element element, final String name)
  9. getSubelementString(final Element element, final String name, final String default_value)