Java XML Attribute Get getAttributeValue(final Node iNode, final String iAttributeName)

Here you can find the source of getAttributeValue(final Node iNode, final String iAttributeName)

Description

This method returns the value of the attribute that matches the attribute name (iAttributeName) and namepace (iNamespaceForAttr) in the node.

License

Open Source License

Parameter

Parameter Description
iNode The element containing the attribute
iAttributeName The name of the attribute being retrieved

Return

The value the attribute

Declaration

public static String getAttributeValue(final Node iNode, final String iAttributeName) 

Method Source Code

//package com.java2s;
/*//from   w  w w  .j  a  va  2  s  .c om
 * Copyright (c) [yyyy] [TITULAR]
 * This file is part of [SSSSS].  
 * 
 * [SSSS] is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version.
 * 
 * This program 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 
 * General Public License for more details, currently published 
 * at http://www.gnu.org/copyleft/gpl.html or in the gpl.txt in 
 * the root folder of this distribution.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
 * 02110-1301, USA.  
 */

import org.w3c.dom.*;

public class Main {
    /**
     * This method returns the value of the attribute that matches the
     * attribute name (iAttributeName) and namepace (iNamespaceForAttr) in
     * the node.  This is to cover cases where elements have multiple
     * attributes that have the same local name but come from different
     * namespaces.
     *
     * @param iNode The element containing the attribute
     * @param iAttributeName The name of the attribute being retrieved
     *
     * @return The value the attribute<br>
     */
    public static String getAttributeValue(final Node iNode, final String iAttributeName) {

        String result = "";
        // Get the attribute from the node matching the attribute name
        // and namespace
        Attr theAttribute = getAttribute(iNode, iAttributeName);

        // Make sure the attribute was present for the element
        if (theAttribute != null) {
            // If present, retrieve the value of the attribute
            result = theAttribute.getValue();
        }

        // return the value
        return result;
    }

    /**
     * This method returns the attribute of the given node whose name matches
     * the named value (iAttributeName) and a particular namespace
     * (iNamespaceForAttr).
     *
     * @param iNode The element containing the attribute
     * @param iAttributeName The name of the attribute being retrieved
     *
     * @return The attribute matching the name and namespace
     */
    public static Attr getAttribute(final Node iNode, final String iAttributeName) {

        Attr result = null;

        // Determine if the node is null
        if (iNode != null) {
            // If the node is not null, then get the list of attributes from
            // the node
            NamedNodeMap attrList = iNode.getAttributes();

            int numAttr = attrList.getLength();

            Attr currentAttrNode = null;
            String currentNodeName = null;

            // Loop through the attributes and get their values assuming
            // that the multiplicity of each attribute is 1 and only 1.
            for (int k = 0; k < numAttr; k++) {
                // Get the attribute
                currentAttrNode = (Attr) attrList.item(k);

                // Get the local name of the attribute
                currentNodeName = currentAttrNode.getLocalName();

                // First check to see if the current node is the one with the
                // same Local Name as the value we are looking for (iAttributeName)
                if (currentNodeName.equalsIgnoreCase(iAttributeName)) {
                    // We have found a node that shares the same name as the
                    // node we are looking for (iAttributeName).
                    // Matching attribute found
                    result = currentAttrNode;
                    break;
                }
            } // end for loop
        }

        return result;
    }
}

Related

  1. getAttributeValue(final Element e, final String attributeName)
  2. getAttributeValue(final Element el, final String attrName)
  3. getAttributeValue(final Element element, final String attributeName)
  4. getAttributeValue(final Element element, final String name)
  5. getAttributeValue(final Node candidate, final String attributName)
  6. getAttributeValue(final Node node, final String attributeName)
  7. getAttributeValue(final Node node, final String name)
  8. getAttributeValue(final XMLStreamReader reader, final String name)
  9. getAttributeValue(NamedNodeMap attributes, String name)