Java XML Attribute Read getNSPrefixFromNSAttr(Attr a)

Here you can find the source of getNSPrefixFromNSAttr(Attr a)

Description

Fetch the non-null namespace prefix from a Attr that declares a namespace.

License

Apache License

Parameter

Parameter Description
a the Attr with the declaration (must be non-<code>null</code).

Return

the namespace prefix or "" if none was declared, e.g., xmlns="foo".

Declaration

public static String getNSPrefixFromNSAttr(Attr a) 

Method Source Code

//package com.java2s;
/*/*  w w  w.  jav  a  2  s.c om*/
 * 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;

public class Main {
    /** The namespaceURI represented by the prefix <code>xmlns</code>. */
    public static final String NS_URI_XMLNS = "http://www.w3.org/2000/xmlns/";

    /**
     * Fetch the non-null namespace prefix from a {@link Attr} that declares
     * a namespace.  (The DOM APIs will return <code>null</code> for a non-prefixed
     * declaration.
     * @param a the {@link Attr} with the declaration (must be non-<code>null</code).
     * @return the namespace prefix or <code>&quot;&quot;</code> if none was
     * declared, e.g., <code>xmlns=&quot;foo&quot;</code>.
     */
    public static String getNSPrefixFromNSAttr(Attr a) {
        assert a != null;
        assert isNSAttribute(a);
        if (a.getPrefix() == null) {
            return "";
        }
        return a.getName().substring(a.getPrefix().length() + 1);
    }

    /**
     * Test whether an attribute contains a namespace declaration.
     * @param a an {@link Attr} to test.
     * @return <code>true</code> if the {@link Attr} is a namespace declaration
     */
    public static boolean isNSAttribute(Attr a) {
        assert a != null;
        String s = a.getNamespaceURI();
        return (s != null && s.equals(NS_URI_XMLNS));
    }
}

Related

  1. getLongAttribute(NamedNodeMap namedNodeMap, String name)
  2. getLongAttribute(String name, Element el)
  3. getNameAttribute(final Node aNode)
  4. getNamedAttribute(final Node aNode, final String attributeName)
  5. getNewAttribute(Element element, String name)
  6. getNullableAttribute(final Element node, final String attributeName)
  7. getNullSafe(NamedNodeMap attr, String key)
  8. getOptionalAttribute(Element elt, String name)
  9. getOptionalAttributeValue(NamedNodeMap attrs, String name)