Java Utililty Methods XML Node Namespace

List of utility methods to do XML Node Namespace

Description

The list of methods to do XML Node Namespace are organized into topic(s).

Method

NodeconvertFromNamespaceForm(final Node node)
convert From Namespace Form
if (node instanceof Element) {
    final Document document = node.getOwnerDocument();
    final Element newElement = document.createElementNS(null, node.getLocalName());
    final NodeList children = node.getChildNodes();
    for (int i = 0, n = children.getLength(); i < n; i++) {
        final Node oldChildNode = children.item(i);
        final Node newChildNode = convertFromNamespaceForm(oldChildNode);
        newElement.appendChild(newChildNode);
...
voidcopyNamespaces(Node source, Node target)
Copies namespace attributes from a source, recursing its parents, to target node.
if (!(target instanceof Element)) {
    return;
Element targetElement = (Element) target;
Node parent = source.getParentNode();
while (parent != null) {
    copyNamespaces(parent, targetElement);
    parent = parent.getParentNode();
...
StringgetNamespace(Node node)
get Namespace
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);
StringgetNamespace(Node node)
Gets the namespace of the given node.
String name = node.getNodeName();
int colon = name.lastIndexOf(":");
return colon < 0 ? null : name.substring(0, colon);
StringgetNamespace(String prefix, Node e)
get Namespace
return getNamespace(prefix, e, null);
MapgetNamespaceDeclarations(Node node)
Build the namespace prefix to namespace URL mapping in effect for a given 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);
            if ("xmlns".equals(attr.getPrefix()) || "xmlns".equals(attr.getName())) {
...
MapgetNamespaceMappings(Node node)
Get all the name spaces visible from this node.
Map<String, String> namespaces = new HashMap<String, String>();
boolean firstLoop = true;
Node n = node;
while (n != null) {
    if (firstLoop && n.getNamespaceURI() != null) {
        namespaces.put("", n.getNamespaceURI());
        firstLoop = false;
    if (n.getAttributes() != null) {
        for (int i = 0; i < n.getAttributes().getLength(); i++) {
            String attrName = n.getAttributes().item(i).getNodeName();
            if (attrName.startsWith("xmlns")) { 
                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);
    n = n.getParentNode();
return namespaces;
voidgetNamespaces(Node node, Map list)
get Namespaces
NamedNodeMap atts = node.getAttributes();
for (int i = 0; i < atts.getLength(); i++) {
    Node n = atts.item(i);
    if ("xmlns".equals(n.getNodeName())) {
        list.put(n.getNodeName(), n.getNodeValue());
    } else {
        if (n.getNodeName().startsWith("xmlns:")) {
            list.put(n.getNodeName().substring(6), n.getNodeValue());
...
StringgetNamespaceURI(@Nullable final Node aNode)
get Namespace URI
if (aNode instanceof Document)
    return getNamespaceURI(((Document) aNode).getDocumentElement());
if (aNode != null)
    return aNode.getNamespaceURI();
return null;
StringgetNamespaceURI(final org.w3c.dom.Node n, final String prefix)
get Namespace URI
final Node prefixDeclaration = n.getAttributes().getNamedItem("xmlns:" + prefix);
if (prefixDeclaration != null) {
    return prefixDeclaration.getNodeValue();
return getNamespaceURI(n.getParentNode(), prefix);