Java XML Child Get by Name getChildElement(Element root, String name)

Here you can find the source of getChildElement(Element root, String name)

Description

getChildElement purpose.

License

Open Source License

Parameter

Parameter Description
root The root element to look for children in.
name The name of the child element to look for.

Return

The child element found, null if not found.

Declaration

public static Element getChildElement(Element root, String name) 

Method Source Code

//package com.java2s;
/*//from  ww w  .  j  av  a  2  s .  c o m
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2004-2008, Open Source Geospatial Foundation (OSGeo)
 *    
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */

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

import org.xml.sax.SAXException;

public class Main {
    /**
     * getChildElement purpose.
     * 
     * <p>
     * Used to help with XML manipulations. Returns the first child element of
     * the specified name.  An exception occurs when the node is required and
     * not found.
     * </p>
     *
     * @param root The root element to look for children in.
     * @param name The name of the child element to look for.
     * @param mandatory true when an exception should be thrown if the child
     *        element does not exist.
     *
     * @return The child element found, null if not found.
     *
     * @throws SAXException When a child element is required and not found.
     */
    public static Element getChildElement(Element root, String name, boolean mandatory) throws SAXException {
        Node child = root.getFirstChild();

        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                if (name.equals(child.getNodeName())) {
                    return (Element) child;
                }
            }

            child = child.getNextSibling();
        }

        if (mandatory && (child == null)) {
            throw new SAXException(root.getNodeName() + " does not contains a child element named " + name);
        }

        return null;
    }

    /**
     * getChildElement purpose.
     * 
     * <p>
     * Used to help with XML manipulations. Returns the first child element of
     * the specified name.
     * </p>
     *
     * @param root The root element to look for children in.
     * @param name The name of the child element to look for.
     *
     * @return The child element found, null if not found.
     *
     * @see getChildElement(Element,String,boolean)
     */
    public static Element getChildElement(Element root, String name) {
        try {
            return getChildElement(root, name, false);
        } catch (SAXException e) {
            //will never be here.
            return null;
        }
    }
}

Related

  1. getChildElement(Element parent, String tagName)
  2. getChildElement(Element parent, String tagName)
  3. getChildElement(Element parentElement, String childElementName)
  4. getChildElement(Element parentElement, String name)
  5. getChildElement(Element parentElement, String tagName)
  6. getChildElement(Element root, String name, boolean mandatory)
  7. getChildElement(final Element element, final String namespace, final String tagName)
  8. getChildElement(final Element parent, final String childName)
  9. getChildElement(final Element parent, final String ns, final String localName)