get DOM Attr by attribute name - Java XML

Java examples for XML:XML Attribute

Description

get DOM Attr by attribute name

Demo Code


//package com.java2s;

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

public class Main {

    public static String getAttr(Node node, String attrName) {
        NamedNodeMap namedNodeMap = node.getAttributes();
        if (namedNodeMap != null) {
            Node attr = namedNodeMap.getNamedItem(attrName);
            if (attr != null) {
                return attr.getNodeValue();
            }/* w ww  .  jav a  2s . co  m*/
        }
        return null;
    }
}

Related Tutorials