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

Here you can find the source of getAttribute(Node iNode, 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

Returns the attribute matching the name and namespace

Declaration

public static Attr getAttribute(Node iNode, String iAttributeName) 

Method Source Code

//package com.java2s;
/* Agrega es una federaci?n de repositorios de objetos digitales educativos formada por todas las Comunidades Aut?nomas propiedad de Red.es. Este c?digo ha sido desarrollado por la Entidad P?blica Empresarial red.es adscrita al Ministerio de Industria,Turismo y Comercio a trav?s de la Secretar?a de Estado de Telecomunicaciones y para la Sociedad de la Informaci?n, dentro del Programa Internet en el Aula, que se encuadra dentro de las actuaciones previstas en el Plan Avanza (Plan 2006-2010 para el desarrollo de la Sociedad de la Informaci?n y de Convergencia con Europa y entre Comunidades Aut?nomas y Ciudades Aut?nomas) y ha sido cofinanciado con fondos FEDER del Programa Operativo FEDER 2000-2006 ?Sociedad de la Informaci?n?
    //from  w ww.j  a  va2 s  .  c  om
This program is free software: you can redistribute it and/or modify it under the terms of the European Union Public Licence (EUPL v.1.0).  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 European Union Public Licence (EUPL v.1.0). You should have received a copy of the EUPL licence along with this program.  If not, see http://ec.europa.eu/idabc/en/document/7330.
*/

import java.util.logging.Logger;
import org.w3c.dom.Attr;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

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 Returns the attribute matching the name and namespace
     */
    public static Attr getAttribute(Node iNode, String iAttributeName) {
        Logger.getLogger("org.adl.util.debug.validator").entering("DOMTreeUtility", "getAttribute()");
        Logger.getLogger("org.adl.util.debug.validator").info("Parent Node: " + iNode.getLocalName());
        Logger.getLogger("org.adl.util.debug.validator").info("Node being searched for: " + 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(Node aNode, String attributeName)
  2. getAttribute(Node attrNode)
  3. getAttribute(Node currentNode, String attributeName)
  4. getAttribute(Node element, String attName)
  5. getAttribute(Node element, String name, String dflt)
  6. getAttribute(Node n, String attr)
  7. getAttribute(Node n, String attr, String def)
  8. getAttribute(Node n, String name)
  9. getAttribute(Node node, String att_name)