Java XML Child Get getChildElements(Element root, String name, boolean mandatory)

Here you can find the source of getChildElements(Element root, String name, boolean mandatory)

Description

getChildElements 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.
mandatory true when an exception should be thrown if the child element does not exist.

Exception

Parameter Description
Exception When a child element is required and notfound.

Return

The child element found, null if not found.

Declaration

public static Element[] getChildElements(Element root, String name,
        boolean mandatory) throws Exception 

Method Source Code

//package com.java2s;
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
 * (c) 2001 - 2013 OpenPlans// w w w. j  a  va2 s.c o  m
 * This code is licensed under the GPL 2.0 license, available at the root
 * application directory.
 */

import java.util.ArrayList;

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

public class Main {
    /**
     * getChildElements purpose.
     *
     * <p>
     * Used to help with XML manipulations. Returns *all* child elements 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[] getChildElements(Element root, String name) {
        try {
            return getChildElements(root, name, false);
        } catch (Exception e) {
            //will never be here.
            return null;
        }
    }

    /**
     * getChildElements purpose.
     *
     * <p>
     * Used to help with XML manipulations. Returns *all* child elements 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 Exception When a child element is required and not
     *         found.
     */
    public static Element[] getChildElements(Element root, String name,
            boolean mandatory) throws Exception {
        ArrayList elements = new ArrayList();
        Node child = root.getFirstChild();

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

            child = child.getNextSibling();
        }

        if (mandatory && (elements.isEmpty())) {
            throw new Exception(root.getNodeName()
                    + " does not contains a child element named " + name);
        }

        return (Element[]) elements.toArray(new Element[0]);
    }
}

Related

  1. getChildElements(Element parent)
  2. getChildElements(Element parentElement)
  3. getChildElements(Element parentElement)
  4. getChildElements(Element parentElement, String childElementTagName)
  5. getChildElements(Element parentNode)
  6. getChildElements(final Element elem)
  7. getChildElements(final Element element)
  8. getChildElements(final Element element, final String localName)
  9. getChildElements(final Element element, final String namespace, final String tagName)