Java XML Attribute Load getAllAttrs(Element parent, String name, List lst)

Here you can find the source of getAllAttrs(Element parent, String name, List lst)

Description

get All Attrs

License

Apache License

Declaration

public static void getAllAttrs(Element parent, String name, List<String> lst) 

Method Source Code


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

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Attr;

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

public class Main {
    public static void getAllAttrs(Element parent, String name, List<String> lst) {
        for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                getAllAttrValueByName(child, name);
                List<String> tmp = getAllAttrValueByName(child, name);
                if (tmp != null) {
                    lst.addAll(tmp);/* w w  w  .j a  v a  2  s .com*/
                } else {
                    // recursive childnodes
                    getAllAttrs((Element) child, name, lst);
                }
            }
        }
    }

    public static List<String> getAllAttrValueByName(Node item, String name) {
        List<String> lst = null;
        NamedNodeMap attributes = item.getAttributes();
        int numAttrs = attributes.getLength();
        for (int i = 0; i < numAttrs; i++) {
            Attr attr = (Attr) attributes.item(i);
            String attrName = attr.getNodeName();
            if (name.equals(attrName)) {
                if (lst == null) {
                    lst = new ArrayList();
                }
                lst.add(attr.getNodeValue());
            }
        }
        return lst;
    }
}

Related

  1. getAllAttributes(Element elem, String name)
  2. getAllAttributes(Element element)
  3. getAllAttributes(Node node)
  4. getAllAttributes(Node node)
  5. getAllAttributesAsMap(@Nullable final Element aSrcNode)
  6. getAllAttrValueByName(Node item, String name)
  7. getAllElementAttributes(Element element)
  8. getAllElementsWithAttrId(final Element element, final String namespace)
  9. loadAttributes(Element e)