Java XML Attribute Get getAttribute(Node n, String attr, String def)

Here you can find the source of getAttribute(Node n, String attr, String def)

Description

get Attribute

License

Open Source License

Parameter

Parameter Description
n Node to examine
attr Attribute to look for
def Default value to return if attribute is not present

Return

if the Node contains the named Attribute, the value, if not, the def parameter

Declaration

public static String getAttribute(Node n, String attr, String def) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**/*from w  ww.  j  a  va2  s.  co m*/
     * @param n Node to examine
     * @param attr Attribute to look for
     * @param def Default value to return if attribute is not present
     * @return if the Node contains the named Attribute, the value, if not, the def parameter
     */
    public static String getAttribute(Node n, String attr, String def) {
        NamedNodeMap attrs = n.getAttributes();
        if (attrs == null)
            return def;
        Node ret = attrs.getNamedItem(attr);
        if (ret == null)
            return def;
        else
            return ret.getNodeValue();
    }

    /**
     * @param n Node to examine
     * @param attr Attribute to look for
     * @return if the Node contains the named Attribute, the value, if not, empty string
     */
    public static String getAttribute(Node n, String attr) {
        return getAttribute(n, attr, "");
    }
}

Related

  1. getAttribute(Node currentNode, String attributeName)
  2. getAttribute(Node element, String attName)
  3. getAttribute(Node element, String name, String dflt)
  4. getAttribute(Node iNode, String iAttributeName)
  5. getAttribute(Node n, String attr)
  6. getAttribute(Node n, String name)
  7. getAttribute(Node node, String att_name)
  8. getAttribute(Node node, String attName, boolean domLevel3)
  9. getAttribute(Node node, String attr)