Java XML Attribute Namespace searchParentNamespaces(Node xml, String attribute)

Here you can find the source of searchParentNamespaces(Node xml, String attribute)

Description

Traverses up the XML Document tree looking for XML an namespace declaration that matches the given attribute.

License

Apache License

Parameter

Parameter Description
xml The Node to start the search from. The search will move up through the parents of this Node.
attribute The name of the XML namespace attribute to search for. This should be <em>xmlns:prefix</em> or <em>targetNamespace</em>.

Return

The first value found for the given attribute. The method returns null if the attribute is not found in the given Node or its parents.

Declaration

private static String searchParentNamespaces(Node xml, String attribute) 

Method Source Code

//package com.java2s;
/* /*ww w  . j  a  v  a2 s  .  com*/
 * 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 javax.xml.namespace.QName;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

public class Main {
    /**
     * 
     * Traverses up the XML Document tree looking for XML an namespace 
     * declaration that matches the given attribute.
     *
     * @param xml
     *        The Node to start the search from. The search will move up 
     *        through the parents of this Node.
     * 
     * @param attribute
     *        The name of the XML namespace attribute to search for. This 
     *        should be <em>xmlns:prefix</em> or <em>targetNamespace</em>.
     * 
     * @return The first value found for the given attribute. The method 
     *         returns null if the attribute is not found in the given Node 
     *         or its parents.
     *
     */
    private static String searchParentNamespaces(Node xml, String attribute) {
        Node next = xml;
        String uri = null;

        //
        // go up the tree until we find a matching prefix/URI (or hit the top)
        //
        while (next != null && (uri == null || uri.length() == 0)) {
            if (next.getNodeType() == Node.ELEMENT_NODE)
                uri = ((Element) next).getAttribute(attribute);

            next = next.getParentNode();
        }

        //
        // DOM returns empty strings for non-existent attributes - we want null
        //
        if (uri != null && uri.length() == 0)
            uri = null;

        return uri;
    }

    public static String getAttribute(Element xml, QName qname) {
        String uri = qname.getNamespaceURI();
        String name = qname.getLocalPart();
        String value = xml.getAttributeNS(uri, name);

        if (value != null && value.length() == 0)
            return null;

        return value;
    }
}

Related

  1. countNonNamespaceAttribures(NamedNodeMap attrs)
  2. getNamespaceDeclarationPrefix(Attr attr)
  3. isNamespaceDef(Attr attr)
  4. isNamespaceDefinition(Attr attr)
  5. isNoNamespaceAttribute(final XMLStreamReader reader, final int index)