Java XML Attribute from Element getIdAttributeValue(Element elem, String name)

Here you can find the source of getIdAttributeValue(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 <N> String getIdAttributeValue(Element elem, String name) 

Method Source Code

//package com.java2s;
/**//from   ww w.  jav  a 2  s . co 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 <N> String getIdAttributeValue(Element elem, String name) {
        Attr attr = elem.getAttributeNodeNS(null, name);
        if (attr != null && !attr.isId()) {
            elem.setIdAttributeNode(attr, true);
        }
        return (attr == null) ? null : attr.getValue();
    }
}

Related

  1. getFirstAttribute(Element elem, String name, String attrName)
  2. getFloatAttribute(Element element, String name)
  3. getFloatAttribute(String name, Element el)
  4. getHeadAttr(Element annotU, String attrName)
  5. getIdAttribute(Element domElement)
  6. getIntAttr(Element elem, String attName)
  7. getIntAttribute(Element el, String name)
  8. getIntAttribute(Element elem, String attName, boolean mandatory, int defaultValue)
  9. getIntAttribute(Element elem, String attName, boolean mandatory, int defaultValue)