Java XML Element Get getElements(Element root, String tagName)

Here you can find the source of getElements(Element root, String tagName)

Description

get Elements

License

Open Source License

Declaration

public static Element[] getElements(Element root, String tagName) 

Method Source Code


//package com.java2s;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Main {
    public static String[] getElements(Element root, String tagName, String attrName) {
        NodeList list = root.getElementsByTagName(tagName);
        String[] result = new String[list.getLength()];
        for (int i = 0; i < list.getLength(); i++) {
            Element el = (Element) list.item(i);
            result[i] = (attrName == null) ? el.getNodeName() : el.getAttribute(attrName);
        }/*from  w w  w.  ja v a  2  s .co m*/

        return (result);
    }

    public static Element[] getElements(Element root, String tagName) {
        NodeList list = root.getElementsByTagName(tagName);
        Element[] result = new Element[list.getLength()];
        for (int i = 0; i < list.getLength(); i++) {
            result[i] = (Element) list.item(i);
        }

        return (result);
    }
}

Related

  1. getElements(Element element)
  2. getElements(Element element, String tagName)
  3. getElements(Element node)
  4. getElements(Element root)
  5. getElements(Element root, String element)
  6. getElements(Element topElm)
  7. getElements(final String elementName, final InputStream is, final boolean onlyValues)
  8. getElementsByName(Element elem, String s)
  9. getElementsByTag(Element element, String tag)