Java XML Node Namespace getNamespaceDeclarations(Node node)

Here you can find the source of getNamespaceDeclarations(Node node)

Description

Build the namespace prefix to namespace URL mapping in effect for a given node.

License

EUPL

Parameter

Parameter Description
node The context node for which build the map.

Return

The namespace prefix to namespace URL mapping ( a String value to String value mapping).

Declaration

public static Map getNamespaceDeclarations(Node node) 

Method Source Code

//package com.java2s;
/*/*from   ww  w  .j av a 2  s .  com*/
 * Copyright 2003 Federal Chancellery Austria
 * MOA-ID has been developed in a cooperation between BRZ, the Federal
 * Chancellery Austria - ICT staff unit, and Graz University of Technology.
 *
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
 * the European Commission - subsequent versions of the EUPL (the "Licence");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 * http://www.osor.eu/eupl/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 *
 * This product combines work with different licenses. See the "NOTICE" text
 * file for details on the various modules and licenses.
 * The "NOTICE" text file is part of the distribution. Any derivative works
 * that you distribute must include a readable copy of the "NOTICE" text file.
 */

import java.util.HashMap;

import java.util.Map;

import org.w3c.dom.Attr;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    /**
     * Build the namespace prefix to namespace URL mapping in effect for a given
     * node.
     * 
     * @param node The context node for which build the map.
     * @return The namespace prefix to namespace URL mapping (
     * a <code>String</code> value to <code>String</code> value mapping).
     */
    public static Map getNamespaceDeclarations(Node node) {
        Map nsDecls = new HashMap();
        int i;

        do {
            if (node.hasAttributes()) {
                NamedNodeMap attrs = node.getAttributes();

                for (i = 0; i < attrs.getLength(); i++) {
                    Attr attr = (Attr) attrs.item(i);

                    // add prefix mapping if none exists
                    if ("xmlns".equals(attr.getPrefix()) || "xmlns".equals(attr.getName())) {

                        String nsPrefix = attr.getPrefix() != null ? attr.getLocalName() : "";

                        if (nsDecls.get(nsPrefix) == null) {
                            nsDecls.put(nsPrefix, attr.getValue());
                        }
                    }
                }
            }
        } while ((node = node.getParentNode()) != null);

        return nsDecls;
    }
}

Related

  1. convertFromNamespaceForm(final Node node)
  2. copyNamespaces(Node source, Node target)
  3. getNamespace(Node node)
  4. getNamespace(Node node)
  5. getNamespace(String prefix, Node e)
  6. getNamespaceMappings(Node node)
  7. getNamespaces(Node node, Map list)
  8. getNamespaceURI(@Nullable final Node aNode)
  9. getNamespaceURI(final org.w3c.dom.Node n, final String prefix)