Java XML Attribute Get getAttributeValueByName(Node node, String name)

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

Description

Returns the attribute value for the passed name in the passed node.

License

Open Source License

Parameter

Parameter Description
node the node to get the attribute from
name the name of the attribute

Return

The attribute value or null if it is not found.

Declaration

public static String getAttributeValueByName(Node node, String name) 

Method Source Code

//package com.java2s;
/**//  ww  w  .  jav  a2 s .c o m
 * Helios, OpenSource Monitoring
 * Brought to you by the Helios Development Group
 *
 * Copyright 2007, Helios Development Group and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This 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 software 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 software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 
 *
 */

import org.w3c.dom.Attr;

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

public class Main {
    /**
     * 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) {
        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);
    }
}

Related

  1. getAttributeValueAsLong(final Element el, final String attrName, final long defaultVal)
  2. getAttributeValueAsString(Node node)
  3. getAttributeValueBoolean(Node node, String attrName)
  4. getAttributeValueByName(NamedNodeMap nnm, String name)
  5. getAttributeValueByName(NamedNodeMap nnm, String name)
  6. getAttributeValueByName(Node node, String name)
  7. getAttributeValueEmptyNull(Element e, String attributeName)
  8. getAttributeValueIgnoreCase(Element element, String attributeName)
  9. getAttributeValueNS(@Nonnull final Element aElement, @Nullable final String sNamespaceURI, @Nonnull final String sAttrName, @Nullable final String sDefault)