Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Retrieves a Map of the value of child nodes in the form { [Node Name] =>
     * [Node Value] }
     * 
     * @param node
     */
    public static Map<String, String> getChildValueMap(Node node) {
        HashMap<String, String> map = new HashMap<String, String>();

        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node n = children.item(i);
            String nodeName = n.getNodeName();
            String nodeValue = getNodeValue(n);

            map.put(nodeName, nodeValue);
        }

        return map;
    }

    /**
     * Retrieves the value of a node (i.e, its text content)
     * 
     * @param node
     * @return
     */
    public static String getNodeValue(Node node) {
        return node.getTextContent();
    }
}