Java XML Child Get by Name getChildByTagName(Element element, String tagName)

Here you can find the source of getChildByTagName(Element element, String tagName)

Description

get Child By Tag Name

License

Apache License

Declaration

public static Element getChildByTagName(Element element, String tagName) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static Element getChildByTagName(Element element, String tagName) {
        Element result = null;/*  www .j  a va 2s  . c  o m*/
        NodeList childNodes = element.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node n = childNodes.item(i);
            if (!(n instanceof Element)) {
                continue;
            }
            if (((Element) n).getTagName().equals(tagName)) {
                if (result != null) {
                    throw new RuntimeException("Too many elements with tag name " + tagName);
                }
                result = (Element) n;
            }
        }
        return result;
    }
}

Related

  1. getChildByName(Node node, String name)
  2. getChildByName(Node node, String nodeName)
  3. getChildByTagName(Element e, String name)
  4. getChildByTagName(Element element, String childElementName)
  5. getChildByTagName(Element element, String tag)
  6. getChildByTagName(Node parent, String tagname)
  7. getChildData(Element e, String tag)
  8. getChildDouble(final Element parent, final String name)
  9. getChildDoubleByName(Node node, String nodeName, double errValue)