Java XML Node Namespace getNamespaceMappings(Node node)

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

Description

Get all the name spaces visible from this node.

License

Open Source License

Parameter

Parameter Description
node a parameter

Declaration

public static Map<String, String> getNamespaceMappings(Node node) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2008-2013, Linagora/*from   ww  w  . j ava2 s .  c  om*/
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *       Linagora - initial API and implementation
 *******************************************************************************/

import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Node;

public class Main {
    /**
     * Get all the name spaces visible from this node.
     * <p>
     * This method was added because the DOM implementation of the Eclipse
     * XML editor does not handle this correctly.
     * </p>
     * @param node
     * @return
     */
    public static Map<String, String> getNamespaceMappings(Node node) {

        Map<String, String> namespaces = new HashMap<String, String>();
        boolean firstLoop = true;
        Node n = node;
        while (n != null) {

            // Get default name space
            if (firstLoop && n.getNamespaceURI() != null) {
                namespaces.put("", n.getNamespaceURI());
                firstLoop = false;
            }

            // Get other name spaces
            if (n.getAttributes() != null) {
                for (int i = 0; i < n.getAttributes().getLength(); i++) {
                    String attrName = n.getAttributes().item(i).getNodeName();

                    if (attrName.startsWith("xmlns")) { //$NON-NLS-1$
                        String prefix = "";
                        int index = attrName.indexOf(':');
                        if (index != -1 && ++index < attrName.length())
                            prefix = attrName.substring(index);

                        String uri = n.getAttributes().item(i).getNodeValue();
                        namespaces.put(prefix, uri);
                    }
                }
            }

            // Go on with the parent
            n = n.getParentNode();
        }

        return namespaces;
    }

    /**
     * @param node
     * @return the name of the XML node (with no name space element)
     */
    public static String getNodeName(Node node) {

        String name = node.getNamespaceURI() != null ? node.getLocalName() : node.getNodeName();
        if (name.contains(":")) { //$NON-NLS-1$
            String[] parts = name.split(":"); //$NON-NLS-1$
            name = parts[parts.length - 1];
        }

        return name;
    }
}

Related

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