Java XML Attribute Get getAttributeValue(@Nonnull final Element aElement, @Nonnull final String sAttrName)

Here you can find the source of getAttributeValue(@Nonnull final Element aElement, @Nonnull final String sAttrName)

Description

The latest version of XercesJ 2.9 returns an empty string for non existing attributes.

License

Apache License

Parameter

Parameter Description
aElement the source element to get the attribute from
sAttrName the name of the attribute to query

Return

null if the attribute does not exists, the string value otherwise

Declaration

@Nullable
public static String getAttributeValue(@Nonnull final Element aElement, @Nonnull final String sAttrName) 

Method Source Code

//package com.java2s;
/**//www  . jav  a 2 s. c om
 * Copyright (C) 2014-2015 Philip Helger (www.helger.com)
 * philip[at]helger[dot]com
 *
 * Licensed 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 javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.w3c.dom.Attr;

import org.w3c.dom.Element;

public class Main {
    /**
     * The latest version of XercesJ 2.9 returns an empty string for non existing
     * attributes. To differentiate between empty attributes and non-existing
     * attributes, this method returns null for non existing attributes.
     *
     * @param aElement
     *        the source element to get the attribute from
     * @param sAttrName
     *        the name of the attribute to query
     * @return <code>null</code> if the attribute does not exists, the string
     *         value otherwise
     */
    @Nullable
    public static String getAttributeValue(@Nonnull final Element aElement, @Nonnull final String sAttrName) {
        return getAttributeValue(aElement, sAttrName, null);
    }

    /**
     * The latest version of XercesJ 2.9 returns an empty string for non existing
     * attributes. To differentiate between empty attributes and non-existing
     * attributes, this method returns a default value for non existing
     * attributes.
     *
     * @param aElement
     *        the source element to get the attribute from. May not be
     *        <code>null</code>.
     * @param sAttrName
     *        the name of the attribute to query. May not be <code>null</code>.
     * @param sDefault
     *        the value to be returned if the attribute is not present.
     * @return the default value if the attribute does not exists, the string
     *         value otherwise
     */
    @Nullable
    public static String getAttributeValue(@Nonnull final Element aElement, @Nonnull final String sAttrName,
            @Nullable final String sDefault) {
        final Attr aAttr = aElement.getAttributeNode(sAttrName);
        return aAttr == null ? sDefault : aAttr.getValue();
    }
}

Related

  1. getAttributesValues(final StartElement element)
  2. getAttributeTable(Element element)
  3. getAttributeText(final Node node, final String name)
  4. getAttributeTextContent(Node node, String attiribute_name)
  5. getAttributeUri(Element leaf, Element parent, String defaultBaseUri)
  6. getAttributeValue(Element e, String key)
  7. getAttributeValue(Element e, String name)
  8. getAttributeValue(Element el, String attributeName)
  9. getAttributeValue(Element el, String attributeName)