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.Node;

public class Main {
    public static List<String> getValuesFromDocumentByTagAndAttribute(Node parentNode, String tagName,
            String attributeName, String attributeValue) {
        ArrayList<String> values = new ArrayList<String>();

        for (int i = 0; i < parentNode.getChildNodes().getLength(); i++) {
            Node childNode = parentNode.getChildNodes().item(i);

            if (childNode != null) {
                if (childNode.hasAttributes()) {
                    for (int j = 0; j < childNode.getAttributes().getLength(); j++) {
                        Node attribute = childNode.getAttributes().item(j);
                        if (attribute.getNodeName().equals(attributeName)
                                && attribute.getNodeValue().equals(attributeValue)) {
                            values.add(childNode.getTextContent().trim());
                        }
                    }
                }

                if (childNode.hasChildNodes()) {
                    values.addAll(getValuesFromDocumentByTagAndAttribute(childNode, tagName, attributeName,
                            attributeValue));
                }
            }
        }

        return values;
    }
}