Java XML Element Get getElementAttributeValue(Document doc, String tagName, String attributeName)

Here you can find the source of getElementAttributeValue(Document doc, String tagName, String attributeName)

Description

Return the named attribute from the specified element in the (XML) document.

License

Open Source License

Parameter

Parameter Description
doc a parameter
tagName a parameter
attributeName a parameter

Return

the attribute value or null if either the element or the attribute are missing

Declaration

static public String getElementAttributeValue(Document doc,
        String tagName, String attributeName) 

Method Source Code

//package com.java2s;
/*/*from   w  w w  . j  a v a  2  s .  co 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.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Return the named attribute from the specified element in the (XML) document.
     * @param doc
     * @param tagName
     * @param attributeName
     * @return the attribute value or null if either the element or the attribute are missing
     */
    static public String getElementAttributeValue(Document doc,
            String tagName, String attributeName) {
        return getAttributeValue(doc.getElementsByTagName(tagName),
                attributeName);
    }

    /**
     * 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. getElement(SOAPMessage message, String tagname, String nsURI, int whichOne)
  2. getElementArray(Element config, String elementName)
  3. getElementAsBoolean(Element e, String name)
  4. getElementAsFloat(Element e, String name, Float dft)
  5. getElementAsInt(Element element)
  6. getElementBooleanValue(Document document, Element parent, String element)
  7. getElementByClass(Element ele, String name)
  8. getElementByFilter(Document doc, NodeFilter filter)
  9. getElementByID(Element el, String id)