Java XML Child Get getChild(Element parent, String name)

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

Description

Returns the first child of specified Element with specified name.

License

Open Source License

Parameter

Parameter Description
parent an Element
name name of child specified

Return

child or null

Declaration

public static Element getChild(Element parent, String name) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009, 2011 The University of Memphis.  All rights reserved. 
 * This program and the accompanying materials are made available 
 * under the terms of the LIDA Software Framework Non-Commercial License v1.0 
 * which accompanies this distribution, and is available at
 * http://ccrg.cs.memphis.edu/assets/papers/2010/LIDA-framework-non-commercial-v1.0.pdf
 *******************************************************************************/

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

public class Main {
    /**//from www.j  av  a2 s.  com
     * Returns the first child of specified Element with specified name.
     * 
     * @param parent
     *            an {@link Element}
     * @param name
     *            name of child specified
     * @return child or null
     */
    public static Element getChild(Element parent, String name) {
        for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
            if (child instanceof Element && name.equals(child.getNodeName())) {
                return (Element) child;
            }
        }
        return null;
    }
}

Related

  1. getChild(Element node, String tagName)
  2. getChild(Element parent, int i)
  3. getChild(Element parent, String element)
  4. getChild(Element parent, String name)
  5. getChild(Element parent, String name)
  6. getChild(Element parentEl, String name)
  7. getChild(Element root, String... path)
  8. getChild(final Element element, final String tagName)
  9. getChild(final Element parent, final String tag)