Android XML Element Attribute Get getAttributeValue(Element elem, String name)

Here you can find the source of getAttributeValue(Element elem, String name)

Description

Returns the attribute value for the attribute with the specified name.

License

Apache License

Parameter

Parameter Description
elem the element containing the attribute
name the name of the attribute

Return

the attribute value (may be null if unspecified)

Declaration

public static String getAttributeValue(Element elem, String name) 

Method Source Code

//package com.java2s;
/**//from w  ww.j a  v a  2s. c o  m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.w3c.dom.Attr;

import org.w3c.dom.Element;

public class Main {
    /**
     * Returns the attribute value for the attribute with the specified name.
     * Returns null if there is no such attribute, or 
     * the empty string if the attribute value is empty.
     *
     * <p>This works around a limitation of the DOM
     * <code>Element.getAttributeNode</code> method, which does not distinguish
     * between an unspecified attribute and an attribute with a value of
     * "" (it returns "" for both cases).
     *
     * @param elem the element containing the attribute
     * @param name the name of the attribute
     * @return the attribute value (may be null if unspecified)
     */
    public static String getAttributeValue(Element elem, String name) {
        Attr attr = elem.getAttributeNodeNS(null, name);
        return (attr == null) ? null : attr.getValue();
    }
}

Related

  1. getBooleanAttribute(Element el, String name)
  2. getIntAttribute(Element el, String name)
  3. processAnyAttributes(Element element, Map anyAttributes)
  4. getNodeAttrValue(Element parent, String nodeName, String attrName)