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.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    /**
     * Returns a Map of all attributes of the given element with the given namespace.
     * Why can we not create our own NamedNodeMaps? This would save trouble.
     * @param element the elment from which to retrieve the attributes.
     * @param namespaceURI the namespace of the attributes to retrieve.
     * @return a Map containing the attributes names and their values.
     */
    public static Map getAttributesWithNS(Element element, String namespaceURI) {
        Map result = new HashMap();

        NamedNodeMap attributes = element.getAttributes();

        if (attributes == null)
            return result;

        for (int i = 0; i != attributes.getLength(); i++) {
            Node attribute = attributes.item(i);

            if (namespaceURI == null && attribute.getNamespaceURI() == null) {
                result.put(attribute.getNodeName(), attribute.getNodeValue());
            } else if (attribute.getNamespaceURI() != null && attribute.getNamespaceURI().equals(namespaceURI)) {
                result.put(attribute.getNodeName(), attribute.getNodeValue());
            }
        }

        return result;
    }
}