Java XML Element Get getElementByTagName(Element n, String elementName)

Here you can find the source of getElementByTagName(Element n, String elementName)

Description

get Element By Tag Name

License

Apache License

Declaration

public static Element getElementByTagName(Element n, String elementName) 

Method Source Code

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

import java.util.ArrayList;

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

public class Main {
    public static Element getElementByTagName(Element n, String elementName) {
        int sz = n.getChildNodes().getLength();
        ArrayList<Element> elements = new ArrayList<Element>(sz);
        for (int idx = 0; idx < sz; idx++) {
            Node node = n.getChildNodes().item(idx);
            if (node instanceof Element && node.getLocalName().equals(elementName))
                elements.add((Element) node);
        }/*  ww w.j a  v a 2 s . com*/
        if (elements.size() > 0)
            return elements.get(0);
        return null;
    }
}

Related

  1. getElementByTagName(Element elem, String name, String emptyValue)
  2. getElementByTagName(Element element, String name)
  3. getElementByTagName(Element element, String name, int index)
  4. getElementByTagName(Element element, String tag)
  5. getElementByTagName(Element element, String tag)
  6. getElementContent(Element e)
  7. getElementContent(Element elem)
  8. getElementContent(Element elem)
  9. getElementContent(Element element, String defaultStr)