Java XML Attribute from Node getValueForAttribute(Node currentNode, String attributeName)

Here you can find the source of getValueForAttribute(Node currentNode, String attributeName)

Description

Get the value for the given attribute.

License

Open Source License

Parameter

Parameter Description
currentNode Node for which attribute value needs to be determined.
attributeName Name of the attribute.

Return

Return the value for the given attribute. Return null if attribute not present for the given node.

Declaration

public static String getValueForAttribute(Node currentNode, String attributeName) 

Method Source Code

//package com.java2s;
/*/*w w  w  .  jav a 2 s .c  o  m*/
@Copyright (c) 2010-2014 The Regents of the University of California.
All rights reserved.
    
Permission is hereby granted, without written agreement and without
license or royalty fees, to use, copy, modify, and distribute this
software and its documentation for any purpose, provided that the
above copyright notice and the following two paragraphs appear in all
copies of this software.
    
IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
    
THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
ENHANCEMENTS, OR MODIFICATIONS.
    
                                        PT_COPYRIGHT_VERSION_2
                                        COPYRIGHTENDKEY
    
    
 */

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

public class Main {
    /**
     * Get the value for the given attribute.
     *
     * @param currentNode Node for which attribute value needs to be determined.
     * @param attributeName Name of the attribute.
     * @return Return the value for the given attribute. Return null if
     * attribute not present for the given node.
     */
    public static String getValueForAttribute(Node currentNode, String attributeName) {

        NamedNodeMap attributes = currentNode.getAttributes();
        String strCurrentModelName = null;

        if (attributes != null) {
            for (int i = 0; i < attributes.getLength(); i++) {

                Node node = attributes.item(i);
                if (node.getNodeName().equalsIgnoreCase(attributeName)) {
                    strCurrentModelName = node.getNodeValue();
                    break;
                }
            }
        }
        return strCurrentModelName;
    }
}

Related

  1. getNodeMap(NamedNodeMap artifactAttributes)
  2. getNodesByAttributeValue(Node node, String attrName, String attrValue)
  3. getNonEmptyAttribute(Element element, String namespace, String localName)
  4. getStringAttributeOptional(Node node, String attributeName, String valueIfEmpty)
  5. getStringAttributeRequired(Node node, String attributeName)
  6. getXMLDate(Element e, String attrName)
  7. getXMLDate(Element e, String attrName)
  8. getXMLInt(Element e, String attrName)
  9. getXMLInt(Element e, String attrName)