Java XML Attribute from Element getElementArrayString(Element root, String name, String attrib)

Here you can find the source of getElementArrayString(Element root, String name, String attrib)

Description

Takes a number of tags of name 'name' that are children of 'root', and looks for attributes of 'attrib' on them.

License

Open Source License

Declaration

public static String[] getElementArrayString(Element root, String name, String attrib) 

Method Source Code


//package com.java2s;
import org.w3c.dom.*;

import java.util.*;

public class Main {
    /**//from  ww w . ja v  a 2 s  . c o m
     * Takes a number of tags of name 'name' that are children of 'root', and
     * looks for attributes of 'attrib' on them.  Converts attributes to an
     * int and returns in an array.
     */
    public static String[] getElementArrayString(Element root, String name, String attrib) {
        if (root == null)
            return null;

        NodeList nl = root.getChildNodes();
        LinkedList elementCache = new LinkedList();
        int size = nl.getLength();

        for (int i = 0; i < size; i++) {
            Node node = nl.item(i);
            if (!(node instanceof Element))
                continue;
            Element ele = (Element) node;
            if (!ele.getTagName().equals(name))
                continue;

            String valS = ele.getAttribute(attrib);

            elementCache.addLast(valS);
        }

        String[] retArr = new String[elementCache.size()];
        Iterator it = elementCache.iterator();
        int idx = 0;
        while (it.hasNext()) {
            retArr[idx++] = (String) it.next();
        }

        return retArr;
    }
}

Related

  1. getElement(Element root, String tagName, String attrName, String attrValue)
  2. getElementArrayInt(Element root, String name, String attrib)
  3. getElementAttr(Element element, String attr)
  4. getElementAttribute(Element element, String name)
  5. getElementAttribute(Element root, String elementName, String attribute)
  6. getElementAttribute(Element root, String elemName, String att)