Java 怎么使所有XML命名空间声明规范化

2019年7月21日19:55:37Java 怎么使所有XML命名空间声明规范化已关闭评论 542 2875字阅读9分35秒

1.简介

Java 怎么使所有XML命名空间声明规范化?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */



import java.util.HashMap;

import java.util.Map;

import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.DocumentTraversal;
import org.w3c.dom.traversal.NodeFilter;
import org.w3c.dom.traversal.NodeIterator;

public class Main {
    
    public static void canonizeNS(Document document, NamespaceContext nsCtx) {
        DocumentTraversal t = (DocumentTraversal) document;

        
        NodeIterator it = t.createNodeIterator(document,
                NodeFilter.SHOW_ELEMENT, null, true);

        for (Node n = it.nextNode(); n != null; n = it.nextNode()) {
            NamedNodeMap attrs = n.getAttributes();
            for (int i = 0; i < attrs.getLength(); ++i) {
                Node a = attrs.item(i);
                if (XMLConstants.XMLNS_ATTRIBUTE.equals(a.getLocalName())) {
                    String prefix = nsCtx.getPrefix(a.getNodeValue());
                    if (prefix != null) {
                        
                        ((Element) n).removeAttributeNS(
                                XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                                XMLConstants.XMLNS_ATTRIBUTE);
                        --i;

                        
                        
                        ((Element) n)
                                .setAttributeNS(
                                        XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                                        XMLConstants.XMLNS_ATTRIBUTE + ":"
                                                + prefix, a.getNodeValue());

                        n.setPrefix(prefix);

                        NodeIterator nit = t.createNodeIterator(n,
                                NodeFilter.SHOW_ELEMENT, null, true);
                        for (Node nn = nit.nextNode(); nn != null; nn = nit
                                .nextNode()) {

                            if (nn.getPrefix() == null) {
                                nn.setPrefix(prefix);
                            }
                        }
                    }
                }
            }
        }

        
        Map<String, String> nsMap = new HashMap<String, String>();
        Map<String, String> prefixMap = new HashMap<String, String>();

        it = t.createNodeIterator(document, NodeFilter.SHOW_ELEMENT, null,
                true);

        for (Node n = it.nextNode(); n != null; n = it.nextNode()) {
            NamedNodeMap attrs = n.getAttributes();
            for (int i = 0; i < attrs.getLength(); ++i) {
                Node a = attrs.item(i);
                if (XMLConstants.XMLNS_ATTRIBUTE.equals(a.getPrefix())
                        && !nsMap.containsKey(a.getNodeValue())) {

                    nsMap.put(a.getNodeValue(), a.getLocalName());
                }

                String prefix = nsCtx.getPrefix(a.getNodeValue());
                if (prefix != null) {
                    prefixMap.put(a.getLocalName(), prefix);
                } else {
                    prefix = nsMap.get(a.getNamespaceURI());
                    if (prefix != null && !prefix.equals(a.getLocalName())) {
                        prefixMap.put(a.getLocalName(), prefix);
                    }
                }
            }
        }

        
        for (String uri : nsMap.keySet()) {
            String prefix = nsCtx.getPrefix(uri);
            if (prefix != null) {
                nsMap.put(uri, prefix);
            }
        }

        
        
        it = t.createNodeIterator(document, NodeFilter.SHOW_ELEMENT, null,
                true);

        for (Node n = it.nextNode(); n != null; n = it.nextNode()) {
            if (n.getPrefix() != null
                    && prefixMap.containsKey(n.getPrefix())) {
                n.setPrefix(prefixMap.get(n.getPrefix()));
            }

            NamedNodeMap attrs = n.getAttributes();
            for (int i = 0; i < attrs.getLength(); ++i) {
                Node a = attrs.item(i);
                if (XMLConstants.XMLNS_ATTRIBUTE.equals(a.getPrefix())) {
                    ((Element) n).removeAttributeNS(
                            XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                            a.getLocalName());
                    --i;
                } else if (a.getPrefix() != null
                        && prefixMap.containsKey(a.getPrefix())) {
                    a.setPrefix(prefixMap.get(a.getPrefix()));
                }
            }
        }

        
        Element root = document.getDocumentElement();
        for (String uri : nsMap.keySet()) {
            root.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                    XMLConstants.XMLNS_ATTRIBUTE + ":" + nsMap.get(uri),
                    uri);
        }
    }
}

 
继续阅读
编程十万个怎么办