Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.HashMap;

public class Main {
    /**
     * This method creates a map of all of the childern of a node
     * @param root - Parent node
     * @return HashMap key value map of the children of the parent node
     */
    public static HashMap getMap(Node root) {
        HashMap map = new HashMap();
        if (root != null) {
            NodeList children = root.getChildNodes();
            for (int i = 0; i < children.getLength(); i++) {
                Node child = children.item(i);
                String nodeName = child.getNodeName();
                if (!nodeName.equalsIgnoreCase("#comment") && !nodeName.equalsIgnoreCase("#text")) {
                    if (child.getChildNodes().getLength() > 1) {
                        map.put(nodeName, child);
                    } else {
                        Node textChild = child.getFirstChild();
                        if (textChild == null) {
                            map.put(nodeName, null);
                        } else {
                            map.put(nodeName, child.getFirstChild().getNodeValue());
                        }
                    }
                }
            }
        }
        return map;
    }
}