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

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

Description

This method returns the attribute of the given node whose name matches the named value (iAttributeName) and a particular namespace (iNamespaceForAttr).

License

Open Source License

Parameter

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

Return

The attribute matching the name and namespace

Declaration

public static Attr getAttribute(final Node iNode, final String iAttributeName) 

Method Source Code

//package com.java2s;
/*//  w  ww.  j  a v a2 s. co  m
 * 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 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. getAttribute(Element ownerElem, String attrName)
  2. getAttribute(Element parent, String localName)
  3. getAttribute(Element parent, String localName, String namespaceURI)
  4. getAttribute(File file, String attr)
  5. getAttribute(final List elements, final int index)
  6. getAttribute(final Node n, final String attrName, final String defaultValue)
  7. getAttribute(final Node node, final String attribname, final int def)
  8. getAttribute(final Node node, final String attributeName)
  9. getAttribute(final Node node, final String name)