Java XML Attribute Exist hasAttribute(final Node node, final String name)

Here you can find the source of hasAttribute(final Node node, final String name)

Description

Determines if the passed node has an attribute with the passed name

License

Apache License

Parameter

Parameter Description
node The node to inspect
name The name to match

Return

true if the named attribute was found, false otherwise

Declaration

public static boolean hasAttribute(final Node node, final String name) 

Method Source Code

//package com.java2s;
/**/*www .  ja  v a 2  s . co  m*/
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at
    
  http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
 */

import org.w3c.dom.Attr;

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

public class Main {
    /**
     * Determines if the passed node has an attribute with the passed name
     * @param node The node to inspect
     * @param name The name to match
     * @return true if the named attribute was found, false otherwise
     */
    public static boolean hasAttribute(final Node node, final String name) {
        return getAttributeByName(node, name, null) != null;
    }

    /**
     * Returns the attribute value for the passed name in the passed node.
     * @param node the node to get the attribute from
     * @param name the name of the attribute
     * @param defaultValue the value to return if the node did not contain the attribute
     * @return The attribute value or the default value if it is not found.
     */
    public static String getAttributeByName(Node node, String name, String defaultValue) {
        try {
            String val = getAttributeValueByName(node, name);
            if (val != null && !val.trim().isEmpty())
                return val.trim();
        } catch (Exception e) {
            /* No Op */}
        return defaultValue;
    }

    /**
     * Returns the value of a named node attribute in the form of a boolean
    * @param node The node to retrieve the attribute from
    * @param name The name of the attribute
    * @param defaultValue The default value if the attribute cannot be located or converted.
    * @return true or false
    */
    public static boolean getAttributeByName(Node node, String name, boolean defaultValue) {
        if (node == null || name == null)
            return defaultValue;
        try {
            return getAttributeBooleanByName(node.getAttributes(), name);
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * Returns the value of a named node attribute in the form of an int
    * @param node The node to retrieve the attribute from
    * @param name The name of the attribute
    * @param defaultValue The default value if the attribute cannot be located or converted.
    * @return an int
    */
    public static int getAttributeByName(Node node, String name, int defaultValue) {
        if (node == null || name == null)
            return defaultValue;
        try {
            return new Double(getAttributeValueByName(node, name).trim()).intValue();
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * Returns the value of a named node attribute in the form of a long
    * @param node The node to retrieve the attribute from
    * @param name The name of the attribute
    * @param defaultValue The default value if the attribute cannot be located or converted.
    * @return a long
    */
    public static long getAttributeByName(Node node, String name, long defaultValue) {
        if (node == null || name == null)
            return defaultValue;
        try {
            return new Double(getAttributeValueByName(node, name).trim()).longValue();
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * Searches throgh the passed NamedNodeMap for an attribute and returns it if it is found.
     * If it is not found, returns a null.
     * @param nnm NamedNodeMap
     * @param name String
     * @return String
     */
    public static String getAttributeValueByName(NamedNodeMap nnm, String name) {
        if (nnm == null)
            return null;
        for (int i = 0; i < nnm.getLength(); i++) {
            Attr attr = (Attr) nnm.item(i);
            if (attr.getName().equalsIgnoreCase(name)) {
                return attr.getValue();
            }
        }
        return null;
    }

    /**
     * Returns the attribute value for the passed name in the passed node.
     * @param node the node to get the attribute from
     * @param name the name of the attribute
     * @return The attribute value or null if it is not found.
     */
    public static String getAttributeValueByName(Node node, String name) {
        return getAttributeValueByName(node.getAttributes(), name);
    }

    /**
     * Searches throgh the passed NamedNodeMap for an attribute. If it is found, it will try to convert it to a boolean.
     * @param nnm NamedNodeMap
     * @param name String
     * @throws RuntimeException on any failure to parse a boolean
     * @return boolean
     */
    public static boolean getAttributeBooleanByName(NamedNodeMap nnm, String name) throws RuntimeException {
        for (int i = 0; i < nnm.getLength(); i++) {
            Attr attr = (Attr) nnm.item(i);
            if (attr.getName().equalsIgnoreCase(name)) {
                String tmp = attr.getValue().toLowerCase();
                if (tmp.equalsIgnoreCase("true"))
                    return true;
                if (tmp.equalsIgnoreCase("false"))
                    return false;
                throw new RuntimeException("Attribute " + name + " value not boolean:" + tmp);
            }
        }
        throw new RuntimeException("Attribute " + name + " not found.");
    }
}

Related

  1. hasAttribute(Element e, String s)
  2. hasAttribute(Element ele, String attrName)
  3. hasAttribute(Element element, String attribute)
  4. hasAttribute(Element element, String namespace, String name)
  5. hasAttribute(Element element, String value)
  6. hasAttribute(final Node node, final String name)
  7. hasAttribute(Node n, String attr)
  8. hasAttribute(Node node, String attributeName)
  9. hasAttribute(Node node, String attributeName)