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

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

Description

Get a child element with a specific name.

License

Open Source License

Parameter

Parameter Description
name Name of the requested child element.
elem Parent element.

Return

The first child element with the given name, or null if none found.

Declaration

public static Element getChildElement(String name, Element elem) 

Method Source Code


//package com.java2s;
/*  /*from w  ww  . j a va  2s. c  o  m*/
 * XMLUtils.java
 *
 * Copyright (C) August Mayer, 2001-2004. All rights reserved.
 * Please consult the Boone LICENSE file for additional rights granted to you.
 *
 * Created on 09. Dezember 2002, 16:31
 */

import org.w3c.dom.*;

public class Main {
    /**
     * Get a child element with a specific name.
     *
     * @param name Name of the requested child element.
     * @param elem Parent element.
     *
     * @return The first child element with the given name, or null if none found.
     */
    public static Element getChildElement(String name, Element elem) {

        NodeList l = elem.getChildNodes();
        for (int i = 0; i < l.getLength(); i++) {
            Node n = l.item(i);
            if (n instanceof Element && n.getNodeName().equals(name))
                return (Element) n;
        }
        return null;
    }
}

Related

  1. getChildElement(Node parent, String elementName)
  2. getChildElement(Node parent, String name)
  3. getChildElement(Node start, String name)
  4. getChildElement(NodeList childs, Node parent)
  5. getChildElement(String name, Element el)
  6. getChildElements(Element element, String childrenName)
  7. getChildElements(Element element, String namespace, String localName)
  8. getChildElements(Element element, String tag)
  9. getChildElements(Element element, String tagName)