Java XML Attribute from Element getElements(Element root, String tagName, String attrName)

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

Description

get Elements

License

Open Source License

Declaration

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

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);
        }/*w w w.j  a v  a 2s  .c  om*/

        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. getElementAttributeValue(Element element, String attributeName)
  2. getElementBooleanValue(Element element, String attribute)
  3. getElementBooleanValue(Element element, String attribute)
  4. getElementBooleanValue(Element element, String attribute, boolean defaultValue)
  5. getElementByAttr(NodeList elements, String attrName)
  6. getElementsByTagAndAttr(Element parent, String elemName, String attrName, String attrVal)
  7. getElementStringValue(Element element, String attribute)
  8. getElementStringValue(Element element, String attribute)
  9. getElementTextByAttr(Element modsroot, String nodename, String attrname, String attrvalue)