Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

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

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

public class Main {
    /**
     * Parse the childnodes of a node and look for <property> elements with attributes name and value.
     * Example:
     * <node>
     *    <property name='n' value='v'/>
     * <node/>
     * @param n the XML node
     * @return the parsed properties
     */
    public static Map<String, String> parseProperties(Node n) {
        NodeList propertyNodes = n.getChildNodes();
        Map<String, String> properties = new HashMap<String, String>();

        for (int i = 0; i < propertyNodes.getLength(); i++) {
            Node node = propertyNodes.item(i);

            if (node.getNodeName().equals("property")) {
                try {
                    String key = node.getAttributes().getNamedItem("name").getNodeValue();
                    String value = node.getAttributes().getNamedItem("value").getNodeValue();

                    properties.put(key, value);
                } catch (NullPointerException e) {
                    continue;
                }
            }
        }

        return properties;
    }
}