Java XML Attribute Get getAttributeValue(Element element, String attrName)

Here you can find the source of getAttributeValue(Element element, String attrName)

Description

TODO: Case sensitive?

License

Open Source License

Parameter

Parameter Description
element a parameter
attrName a parameter

Return

null if not found

Declaration

public static String getAttributeValue(Element element, String attrName) 

Method Source Code

//package com.java2s;
/**//  ww  w.  jav  a2  s .  c o  m
 * 
 * This file is part of SavoyCraft.
 * 
 * SavoyCraft is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * SavoyCraft 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 Lesser General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with SavoyCraft. If not, see <http://www.gnu.org/licenses/>.
 * 
 */

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

public class Main {
    /**
     * TODO: Case sensitive?
     * 
     * @param element
     * @param attrName
     * @return null if not found
     */
    public static String getAttributeValue(Element element, String attrName) {
        NamedNodeMap attributes = element.getAttributes();
        Node attribute = attributes.getNamedItem(attrName);
        if (attribute == null) {
            return null;
        } else {
            return attribute.getNodeValue();
        }
    }

    /**
     * As getAttributeValue, except that if there is no attribute with the given
     * name, this method returns defaultValue instead.
     * 
     * @param element
     * @param attrName
     * @param defaultValue
     * @return
     */
    public static String getAttributeValue(Element element, String attrName, String defaultValue) {
        String attrValue = getAttributeValue(element, attrName);
        if (attrValue == null) {
            return defaultValue;
        } else {
            return attrValue;
        }
    }
}

Related

  1. getAttributeValue(Element elem, String name)
  2. getAttributeValue(Element element, String attr)
  3. getAttributeValue(Element element, String attributeName)
  4. getAttributeValue(Element element, String attributeName, String namespaceURI)
  5. getAttributeValue(Element element, String attrName)
  6. getAttributeValue(Element element, String name)
  7. getAttributeValue(Element element, String name)
  8. getAttributeValue(Element element, String tag)
  9. getAttributeValue(final Element e, final String attributeName)