Java XML Attribute Get getAttributeValue(NodeList elements, String attributeName)

Here you can find the source of getAttributeValue(NodeList elements, String attributeName)

Description

Return the value of the named attribute from the first Node in the NodeList or null if the NodeList is empty or the attribute is not present.

License

Open Source License

Parameter

Parameter Description
elements a parameter
attributeName a parameter

Return

a String or null

Declaration

static public String getAttributeValue(NodeList elements,
        String attributeName) 

Method Source Code

//package com.java2s;
/*/*ww  w.  jav  a 2 s . c o m*/
 * dalclient library - provides utilities to assist in using KDDart-DAL servers
 * Copyright (C) 2015,2016,2017 Diversity Arrays Technology
 * 
 * This program 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 3 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.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Return the value of the named attribute from the first Node in the NodeList
     * or null if the NodeList is empty or the attribute is not present.
     * @param elements
     * @param attributeName
     * @return a String or null
     */
    static public String getAttributeValue(NodeList elements,
            String attributeName) {
        String result = null;
        if (elements != null && elements.getLength() > 0) {
            Node item = elements.item(0);
            NamedNodeMap attributes = item.getAttributes();
            if (attributes != null) {
                Node attr = attributes.getNamedItem(attributeName);
                if (attr != null) {
                    result = attr.getNodeValue();
                }
            }
        }
        return result;
    }
}

Related

  1. getAttributeValue(Node node, String name)
  2. getAttributeValue(Node node, String name, boolean defaultValue)
  3. getAttributeValue(Node node, String name, String defaultValue)
  4. getAttributeValue(Node sNode, String attribName)
  5. getAttributeValue(Node sNode, String attribName)
  6. getAttributeValue(StartElement element, String namespaceURI, String localPart)
  7. getAttributeValue(StartElement startElement, String attributeName)
  8. getAttributeValue(String attributeName, Node xmlNode)
  9. getAttributeValueAsBoolean(Attr attribute)