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 {
    public static Node getChild(Node parent, String nodename) {
        for (Node child : toList(parent.getChildNodes())) {
            if (child.getNodeName().equalsIgnoreCase(nodename)) {
                return child;
            }
        }
        return null;
    }

    /**
     * Convert a {@link NodeList} to a {@link List}
     *
     * @param nodelist Node list
     * @return {@link List} of nodes
     */
    public static List<Node> toList(NodeList nodelist) {
        ArrayList<Node> list = new ArrayList<Node>(nodelist.getLength());
        for (int i = 0; i < nodelist.getLength(); i++) {
            list.add(nodelist.item(i));
        }
        return list;
    }

    /**
     * Convert a {@link NamedNodeMap} contains attribute nodes {@link Attr}
     *
     * @param attrList {@link NamedNodeMap} containing {@link Attr} objects exclusively
     * @return List of {@link Attr}.
     */
    public static List<Attr> toList(NamedNodeMap attrList) {
        ArrayList<Attr> list = new ArrayList<Attr>(attrList.getLength());
        for (int i = 0; i < attrList.getLength(); i++) {
            list.add((Attr) attrList.item(i));
        }
        return list;
    }

    @SuppressWarnings({ "unchecked" })
    public static <X extends Node> List<X> getChildNodes(Node node, Class<X> type) {
        final NodeList nodelist = node.getChildNodes();
        ArrayList<X> list = new ArrayList<X>(nodelist.getLength());
        for (int i = 0; i < nodelist.getLength(); i++) {
            final Node child = nodelist.item(i);
            if (type.isInstance(child)) {
                list.add((X) child);
            }
        }
        return list;
    }
}