Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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);
                } 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;
    }
}