Java XML Node Namespace convertFromNamespaceForm(final Node node)

Here you can find the source of convertFromNamespaceForm(final Node node)

Description

convert From Namespace Form

License

Open Source License

Declaration

private static Node convertFromNamespaceForm(final Node node) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2015 Oracle/*ww  w  .  ja v  a2 s.c  o m*/
 * 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:
 *    Konstantin Komissarchik - initial implementation and ongoing maintenance
 ******************************************************************************/

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static final String XMLNS = "xmlns";
    public static final String XMLNS_COLON = "xmlns:";
    public static final String XSI_SCHEMA_LOCATION_ATTR = "schemaLocation";

    /**
     * Converts the document from namespace-qualified form into unqualified form by removing all 
     * namespace information.
     * 
     * @param document the document to convert
     */

    public static void convertFromNamespaceForm(final Document document) {
        final Element oldRootElement = document.getDocumentElement();

        if (oldRootElement != null) {
            final Node nodeAfterRootElement = oldRootElement.getNextSibling();
            final Node newRootElement = convertFromNamespaceForm(oldRootElement);
            document.removeChild(oldRootElement);
            document.insertBefore(newRootElement, nodeAfterRootElement);
        }
    }

    private static Node convertFromNamespaceForm(final Node node) {
        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);
            }

            final NamedNodeMap attributes = node.getAttributes();

            for (int i = 0, n = attributes.getLength(); i < n; i++) {
                final Attr attr = (Attr) attributes.item(i);
                final String attrQualifiedName = attr.getNodeName();
                final String attrLocalName = attr.getLocalName();

                if (!attrQualifiedName.equals(XMLNS) && !attrQualifiedName.startsWith(XMLNS_COLON)
                        && !attrLocalName.equals(XSI_SCHEMA_LOCATION_ATTR)) {
                    newElement.setAttributeNS(null, attrLocalName, attr.getValue());
                }
            }

            return newElement;
        } else {
            return node.cloneNode(true);
        }
    }
}

Related

  1. copyNamespaces(Node source, Node target)
  2. getNamespace(Node node)
  3. getNamespace(Node node)
  4. getNamespace(String prefix, Node e)