Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;

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

public class Main {
    /**
     * 
     * @param doc
     * @param strTagName
     * @return
     */
    public static ArrayList<String> getArrayListFromNodes(Document doc, String strTagName) {

        ArrayList<String> alValues = new ArrayList<String>();

        NodeList nl = doc.getElementsByTagName(strTagName);

        // if there are tags
        if (nl != null) {

            // cycles on all of them
            for (int i = 0; i < nl.getLength(); i++) {

                // if it is a field Node
                if (nl.item(i).getNodeType() == 1) {

                    // gets the i-th element
                    Element e = (Element) nl.item(i);

                    // if it has a value adds it to the AL
                    if (e.getFirstChild() != null)
                        alValues.add(e.getFirstChild().getNodeValue());
                }
            }
        }

        return alValues;
    }
}