Java XML Node Namespace getNamespace(Node node)

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

Description

get Namespace

License

Open Source License

Declaration

public static String getNamespace(Node node) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.HashMap;

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

public class Main {
    public static String getNamespace(Node node) {
        Element root = node.getOwnerDocument().getDocumentElement();
        HashMap<String, String> namespaces = getAllAttributes(root);
        String prefix = getPrefix(node.getNodeName());

        if (prefix == null)
            return namespaces.get("xmlns");

        return namespaces.get("xmlns:" + prefix);
    }/*w  ww . j av a2  s  .c o m*/

    public static HashMap<String, String> getAllAttributes(Node node) {
        HashMap<String, String> attributes = new HashMap<String, String>();
        NamedNodeMap attr = node.getAttributes();
        for (int j = 0; j < attr.getLength(); j++) {
            attributes.put(attr.item(j).getNodeName(), attr.item(j).getNodeValue());
        }
        return attributes;
    }

    public static String getPrefix(String value) {
        try {
            return value.substring(0, value.indexOf(":"));
        } catch (Exception e) {
        }
        return null;
    }
}

Related

  1. convertFromNamespaceForm(final Node node)
  2. copyNamespaces(Node source, Node target)
  3. getNamespace(Node node)
  4. getNamespace(String prefix, Node e)
  5. getNamespaceDeclarations(Node node)
  6. getNamespaceMappings(Node node)