get XML Element Attributes - Android XML

Android examples for XML:XML Attribute

Description

get XML Element Attributes

Demo Code


//package com.java2s;
import java.util.HashMap;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    public static HashMap<String, String> getElementAttributes(
            Element element) {/*w w w  .  ja  va 2  s .co m*/
        HashMap<String, String> attributes = new HashMap<String, String>();

        NamedNodeMap map = element.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            Node node = map.item(i);
            if (node.getNodeType() == Node.ATTRIBUTE_NODE)
                attributes.put(node.getNodeName(), node.getNodeValue());
        }

        return attributes;
    }

    public static String getNodeValue(Node node) {
        String value = "";

        try {
            if (node.getFirstChild() != null)
                value = node.getFirstChild().getNodeValue();
        } catch (Exception e) {
            value = null;
        }

        return value;
    }
}

Related Tutorials