XML DOM NamedNodeMap to Map - Java XML

Java examples for XML:XML Node

Description

XML DOM NamedNodeMap to Map

Demo Code


//package com.java2s;

import java.util.HashMap;

import java.util.Map;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    public static Map<String, String> toMap(NamedNodeMap attrs) {
        return toMapExcept(attrs);
    }/*from  www.  j a v  a  2s .c  o m*/

    public static Map<String, String> toMapExcept(NamedNodeMap attrs,
            String... exclusions) {
        Map<String, String> args = new HashMap<String, String>();
        outer: for (int j = 0; j < attrs.getLength(); j++) {
            Node attr = attrs.item(j);
            String attrName = attr.getNodeName();
            for (String ex : exclusions)
                if (ex.equals(attrName))
                    continue outer;
            String val = attr.getNodeValue();
            args.put(attrName, val);
        }
        return args;
    }
}

Related Tutorials