Java XML Child Get by Name getChildDouble(final Element parent, final String name)

Here you can find the source of getChildDouble(final Element parent, final String name)

Description

Given a parent element, locate double value of a child node.

License

Open Source License

Parameter

Parameter Description
parent Parent element
name Name of child element

Exception

Parameter Description
Exception on error parsing the number

Return

Value of child element, or empty result

Declaration

public static Optional<Double> getChildDouble(final Element parent, final String name) throws Exception 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015-2016 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 java.util.Optional;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

public class Main {
    /** Given a parent element, locate double value of a child node.
     *  @param parent Parent element/*  w  w w. jav  a 2s  .c o  m*/
     *  @param name Name of child element
     *  @return Value of child element, or empty result
     *  @throws Exception on error parsing the number
     */
    public static Optional<Double> getChildDouble(final Element parent, final String name) throws Exception {
        final Element child = getChildElement(parent, name);
        if (child == null)
            return Optional.empty();
        try {
            return Optional.of(Double.valueOf(getString(child)));
        } catch (NumberFormatException ex) {
            throw new Exception("Expected double for <" + name + ">", ex);
        }
    }

    /** Look for child node of given name.
     *
     *  @param parent Node where to start.
     *  @param name Name of the node to look for.
     *  @return Returns Element or <code>null</code>.
     */
    public static final Element getChildElement(final Node parent, final String name) {
        return findElementByName(parent.getFirstChild(), name);
    }

    /** Get string value of an element.
     *  @param element Element
     *  @return String of the node. Empty string if nothing found.
     */
    public static String getString(final Element element) {
        final Node text = element.getFirstChild();
        if (text == null) // <empty /> node
            return "";
        if ((text.getNodeType() == Node.TEXT_NODE || text.getNodeType() == Node.CDATA_SECTION_NODE))
            return text.getNodeValue();
        return "";
    }

    /** Look for Element node of given name.
     *
     *  <p>Checks the node itself and its siblings for an {@link Element}.
     *  Does not descent down the 'child' links.
     *
     *  @param node Node where to start.
     *  @param name Name of the node to look for.
     *  @return Returns node, the next matching sibling, or <code>null</code>.
     */
    private static final Element findElementByName(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. getChildByTagName(Element element, String childElementName)
  2. getChildByTagName(Element element, String tag)
  3. getChildByTagName(Element element, String tagName)
  4. getChildByTagName(Node parent, String tagname)
  5. getChildData(Element e, String tag)
  6. getChildDoubleByName(Node node, String nodeName, double errValue)
  7. getChildElemByTagName(Element parent, String tagName)
  8. getChildElement(Element currentNode, String nsURI, String localName)
  9. getChildElement(Element el, String tagName)