Java XML Attribute Get getAttribute(Node element, String name, String dflt)

Here you can find the source of getAttribute(Node element, String name, String dflt)

Description

Get the given name-d attribute from the given element.

License

Open Source License

Parameter

Parameter Description
element The xml element to look within.
name The attribute name.
dflt The default value.

Return

The attribute value or the dflt if not found.

Declaration

public static String getAttribute(Node element, String name, String dflt) 

Method Source Code

//package com.java2s;
/*//from w  w w. j a  v a  2s  .  com
 * Copyright 1997-2016 Unidata Program Center/University Corporation for
 * Atmospheric Research, P.O. Box 3000, Boulder, CO 80307,
 * support@unidata.ucar.edu.
 * 
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

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

public class Main {
    /**
     *  Get the given name-d attribute from the given element. If not found
     *  return the dflt argument.
     *
     *  @param element The xml element to look within.
     *  @param name The attribute name.
     *  @param dflt The default value.
     *  @return The attribute value or the dflt if not found.
     */
    public static String getAttribute(Node element, String name, String dflt) {
        if (element == null) {
            return dflt;
        }
        return getAttribute(element.getAttributes(), name, dflt);
    }

    /**
     * _more_
     *
     * @param attrs _more_
     * @param name _more_
     * @param dflt _more_
     *
     * @return _more_
     */
    public static String getAttribute(NamedNodeMap attrs, String name, String dflt) {
        if (attrs == null) {
            return dflt;
        }
        Node n = attrs.getNamedItem(name);
        return ((n == null) ? dflt : n.getNodeValue());
    }

    /**
     * _more_
     *
     * @param attrs _more_
     * @param name _more_
     * @param dflt _more_
     *
     * @return _more_
     */
    public static boolean getAttribute(NamedNodeMap attrs, String name, boolean dflt) {
        if (attrs == null) {
            return dflt;
        }
        Node n = attrs.getNamedItem(name);
        return ((n == null) ? dflt : new Boolean(n.getNodeValue()).booleanValue());
    }

    /**
     * _more_
     *
     * @param attrs _more_
     * @param name _more_
     *
     * @return _more_
     */
    public static String getAttribute(NamedNodeMap attrs, String name) {
        String value = getAttribute(attrs, name, (String) null);
        if (value == null) {
            throw new IllegalArgumentException("Could not find xml attribute:" + name);
        }
        return value;
    }
}

Related

  1. getAttribute(NamedNodeMap ruleAttributes, String attributeName)
  2. getAttribute(Node aNode, String attributeName)
  3. getAttribute(Node attrNode)
  4. getAttribute(Node currentNode, String attributeName)
  5. getAttribute(Node element, String attName)
  6. getAttribute(Node iNode, String iAttributeName)
  7. getAttribute(Node n, String attr)
  8. getAttribute(Node n, String attr, String def)
  9. getAttribute(Node n, String name)