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 {
    /**
     * @param node
     * @param name
     * @return a map with all children nodes with the given name
     * mapped by the value of their first child
     */
    public static Map<String, Node> findChildrenMapByFirstChild(Node node, String name) {
        Map<String, Node> ret = new HashMap<String, Node>();
        NodeList nl = node.getChildNodes();
        int len = nl.getLength();
        Node child;
        for (int i = 0; i < len; i++) {
            child = nl.item(i);
            if (name.equals(child.getNodeName())) {
                ret.put(child.getFirstChild().getNodeValue(), child);
            }
        }
        return ret;
    }
}