Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

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

public class Main {
    /**
     * Return all values associated with attributeName inside element specified by tag
     * @param document doc we work with
     * @param elementTag element containing desired attribute
     * @param attributeName name of attribute
     * @return List of values specified in XML array
     */
    public static List<String> getListOfTagAttributeValues(Document document, String elementTag,
            String attributeName) {
        NodeList usesPermissionList = document.getElementsByTagName(elementTag);

        List<String> result = new ArrayList<String>();
        for (int i = 0; i < usesPermissionList.getLength(); i++) {
            Node nNode = usesPermissionList.item(i);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                String value = eElement.getAttribute(attributeName);
                if (value != null && !value.isEmpty()) {
                    result.add(value);
                }
            }
        }
        return result;
    }
}