Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.w3c.dom.*;

import java.util.*;

public class Main {
    /**
     * Takes a number of tags of name 'name' that are children of 'root', and
     * looks for attributes of 'attrib' on them.  Converts attributes to an
     * int and returns in an array.
     */
    public static String[] getElementArrayString(Element root, String name, String attrib) {
        if (root == null)
            return null;

        NodeList nl = root.getChildNodes();
        LinkedList<String> elementCache = new LinkedList<String>();
        int size = nl.getLength();

        for (int i = 0; i < size; i++) {
            Node node = nl.item(i);
            if (!(node instanceof Element))
                continue;
            Element ele = (Element) node;
            if (!ele.getTagName().equals(name))
                continue;

            String valS = ele.getAttribute(attrib);

            elementCache.addLast(valS);
        }

        String[] retArr = new String[elementCache.size()];
        Iterator<String> it = elementCache.iterator();
        int idx = 0;
        while (it.hasNext()) {
            retArr[idx++] = it.next();
        }

        return retArr;
    }
}