Java XML First Child Element xmlFirstChild(final Element parent, final String childName, final boolean isThrow)

Here you can find the source of xmlFirstChild(final Element parent, final String childName, final boolean isThrow)

Description

Xml first child.

License

BSD License

Parameter

Parameter Description
parent the parent
childName the child name
isThrow the is throw

Return

the element

Declaration

public static final Element xmlFirstChild(final Element parent,
        final String childName, final boolean isThrow) 

Method Source Code

//package com.java2s;
/**//from  w  w w  .  j a  v a2  s .c o m
 * Copyright (C) 2011-2012 Barchart, Inc. <http://www.barchart.com/>
 *
 * All rights reserved. Licensed under the OSI BSD License.
 *
 * http://www.opensource.org/licenses/bsd-license.php
 */

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

public class Main {
    /**
     * Xml first child.
     * 
     * @param parent
     *            the parent
     * @param childName
     *            the child name
     * @param isThrow
     *            the is throw
     * @return the element
     */
    public static final Element xmlFirstChild(final Element parent,
            final String childName, final boolean isThrow) {
        final NodeList nodeList = parent.getChildNodes();
        if (nodeList == null) {
            if (isThrow) {
                throw new IllegalArgumentException("nodeList == null");
            }
            return null;
        }
        final int size = nodeList.getLength();
        for (int k = 0; k < size; k++) {
            final Node child = nodeList.item(k);
            if (child.getNodeName().equalsIgnoreCase(childName)) {
                return (Element) child;
            }
        }
        if (isThrow) {
            throw new IllegalArgumentException("child node not found");
        }
        return null;
    }
}

Related

  1. getFirstVisibleChildElement(Node parent)
  2. getValueOfFirstElementChild(final Element element, final String namespace, final String localname)
  3. insertBeforeFirstChild(Element object, Element attrId)
  4. readFirstChild(Node parentNode, String nodeName)
  5. VectoriseFirstChildrenNodes(Node node, Vector elements)