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

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

Returns the value the attribute

Declaration

public static String getAttributeValue(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  w  w  .  j  av a2 s  .  c  o  m
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 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 Returns the value the attribute<br>
     */
    public static String getAttributeValue(Node iNode, String iAttributeName) {
        Logger.getLogger("org.adl.util.debug.validator").entering("DOMTreeUtility", "getAttributeValue()");
        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);
        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 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. getAttributeValue(final XMLStreamReader reader, final String name)
  2. getAttributeValue(NamedNodeMap attributes, String name)
  3. getAttributeValue(Node element, String attribute)
  4. getAttributeValue(Node element, String attribute)
  5. getAttributeValue(Node htmlForm, String attributeName)
  6. getAttributeValue(Node n, String name)
  7. getAttributeValue(Node n, String name)
  8. getAttributeValue(Node n, String nodePath, String attrName)
  9. getAttributeValue(Node node, String attName)