Java XML Attribute Get getAttributeValueNS(@Nonnull final Element aElement, @Nullable final String sNamespaceURI, @Nonnull final String sAttrName, @Nullable final String sDefault)

Here you can find the source of getAttributeValueNS(@Nonnull final Element aElement, @Nullable final String sNamespaceURI, @Nonnull final String sAttrName, @Nullable final String sDefault)

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. May not be <code>null</code>.
sNamespaceURI The namespace URI of the attribute to retrieve. May be <code>null</code>.
sAttrName the name of the attribute to query. May not be <code>null</code>.
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

Declaration

@Nullable
public static String getAttributeValueNS(@Nonnull final Element aElement, @Nullable final String sNamespaceURI,
        @Nonnull final String sAttrName, @Nullable final String sDefault) 

Method Source Code

//package com.java2s;
/**//from  www  .  j ava2  s. c o  m
 * Copyright (C) 2014-2017 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 sNamespaceURI
     *        The namespace URI of the attribute to retrieve. May be
     *        <code>null</code>.
     * @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 getAttributeValueNS(@Nonnull final Element aElement, @Nullable final String sNamespaceURI,
            @Nonnull final String sAttrName) {
        return getAttributeValueNS(aElement, sNamespaceURI, 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 sNamespaceURI
     *        The namespace URI of the attribute to retrieve. May 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 getAttributeValueNS(@Nonnull final Element aElement, @Nullable final String sNamespaceURI,
            @Nonnull final String sAttrName, @Nullable final String sDefault) {
        final Attr aAttr = aElement.getAttributeNodeNS(sNamespaceURI, sAttrName);
        return aAttr == null ? sDefault : aAttr.getValue();
    }
}

Related

  1. getAttributeValueByName(NamedNodeMap nnm, String name)
  2. getAttributeValueByName(Node node, String name)
  3. getAttributeValueByName(Node node, String name)
  4. getAttributeValueEmptyNull(Element e, String attributeName)
  5. getAttributeValueIgnoreCase(Element element, String attributeName)
  6. getAttributeValueNS(Element element, String namespace, String attrName)
  7. getAttributeValueOrNull(NamedNodeMap attributes, String attributeName)
  8. getAttributeValuePair(Node node)
  9. getAttributeValues(Element el)