fetch the XML attribute value from the given element node - Android XML

Android examples for XML:XML Attribute

Description

fetch the XML attribute value from the given element node

Demo Code

// Copyright (c) 2004-2008 IBM Corporation and others. All Rights Reserved.
//package com.java2s;
import org.w3c.dom.*;

public class Main {
    /**/*  ww w. j  a va2  s.  co m*/
     * Utility method to fetch the attribute value from the given 
     * element node
     * @param sNode
     * @param attribName
     * @return
     */
    public static String getAttributeValue(Node sNode, String attribName) {
        String value = null;
        NamedNodeMap attrs = sNode.getAttributes();
        if (attrs != null) {
            Node attr = attrs.getNamedItem(attribName);
            if (attr != null) {
                value = attr.getNodeValue();
            }
        }
        return value;
    }

    /**
     * Utility method to fetch the value of the element node
     * @param node
     * @return
     */
    public static String getNodeValue(Node node) {
        for (Node child = node.getFirstChild(); child != null; child = child
                .getNextSibling()) {
            if (child.getNodeType() == Node.TEXT_NODE) {
                return child.getNodeValue();
            }
        }
        return null;
    }
}

Related Tutorials