Java XML Attribute Get getAttr(final Node n, final String attrName)

Here you can find the source of getAttr(final Node n, final String attrName)

Description

Gives the value of a given attribute

License

Open Source License

Parameter

Parameter Description
n A <code>Node</code> which holds the attribute
attrName The name of the attribute

Return

The value of the attribute or null if the attribute does not exsist

Declaration

public static String getAttr(final Node n, final String attrName) 

Method Source Code


//package com.java2s;
/* License as published by the Free Software Foundation; either         */

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

public class Main {
    /**//from  w  ww.java 2s  .  c om
     * Gives the value of a given attribute
     * 
     * @param n A <code>Node</code> which holds the attribute
     * @param attrName The name of the attribute
     * @return The value of the attribute or <code>null</code> if the attribute does not exsist
     */
    public static String getAttr(final Node n, final String attrName) {
        String value = null;
        if (n.hasAttributes()) {
            final NamedNodeMap map = n.getAttributes();
            for (int i = 0; i < map.getLength(); i++) {
                final Node attrNode = map.item(i);
                if (attrNode.getNodeName().equals(attrName)) {
                    value = attrNode.getNodeValue();
                }
            }
        }

        return value;
    }
}

Related

  1. getAtrributeValue(Node node, String attribute)
  2. getAttr(Element e, String name)
  3. getAttr(Element elem, String name)
  4. getAttr(final Node n, final String attr)
  5. getAttr(NamedNodeMap attrs, String name)
  6. getAttr(NamedNodeMap attrs, String name, String missing_err)
  7. getAttr(Node n, String name)
  8. getAttr(Node node, String attr)