Java XML Node Serialize serializeNoNS(Node node)

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

Description

Serializes a Node to a String

License

Open Source License

Declaration

public static String serializeNoNS(Node node) 

Method Source Code

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

import java.util.HashMap;

import java.util.Map;
import java.util.Map.Entry;
import javax.xml.namespace.QName;

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

import org.w3c.dom.Attr;

public class Main {
    /**//from   w  w w .ja  v a  2 s  .c o m
     * Serializes a Node to a String
     */
    public static String serializeNoNS(Node node) {
        StringBuffer buf = new StringBuffer();
        buf.append("<");
        buf.append(node.getLocalName());
        for (Entry<QName, String> entry : getAttributes(node).entrySet()) {
            QName name = entry.getKey();
            if (name.getNamespaceURI() != null) {
                buf.append(" ");
                buf.append(name.getLocalPart());
                buf.append("=\"");
                buf.append(entry.getValue());
                buf.append("\"");
            }
        }
        boolean tagOpen = true;
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node n = children.item(i);
            short type = node.getNodeType();
            if (type == Node.TEXT_NODE) {
                if (tagOpen) {
                    buf.append(">\n");
                    tagOpen = false;
                }
                buf.append(node.getTextContent());
            } else if (type == Node.ELEMENT_NODE) {
                if (tagOpen) {
                    buf.append(">\n");
                    tagOpen = false;
                }
                buf.append(serializeNoNS(n));
                buf.append("\n");
            }
        }
        if (tagOpen) {
            buf.append("/>\n");
        } else {
            buf.append("</");
            buf.append(node.getLocalName());
            buf.append(">\n");
        }
        return buf.toString();
        // ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // try {
        // TransformerFactory factory = TransformerFactory.newInstance();
        // File f = Misc.getResourceAsFile("com/occamlab/te/drop-ns.xsl");
        // Transformer transformer = factory.newTransformer(new
        // StreamSource(f));
        //
        // DOMSource src = new DOMSource(node);
        // StreamResult dest = new StreamResult(baos);
        // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
        // "yes");
        // transformer.transform(src, dest);
        // } catch (Exception e) {
        // System.out.println("Error serializing node.  "+e.getMessage());
        // }
        //
        // return baos.toString();
    }

    static public Map<QName, String> getAttributes(Node node) {
        Map<QName, String> atts = new HashMap<QName, String>();
        NamedNodeMap nnm = node.getAttributes();
        if (nnm != null) {
            for (int i = 0; i < nnm.getLength(); i++) {
                Attr att = (Attr) nnm.item(i);
                String uri = att.getBaseURI();
                String localname = att.getLocalName();
                String prefix = att.getPrefix();
                QName name;
                if (uri == null) {
                    name = new QName(localname);
                } else if (prefix == null) {
                    name = new QName(uri, localname);
                } else {
                    name = new QName(uri, localname, prefix);
                }
                if (prefix == null || !(prefix.equals("xmlns") || prefix.equals("xml"))) {
                    atts.put(name, att.getValue());
                }
            }
        }
        return atts;
    }
}

Related

  1. serializeNode(Node node)
  2. serializeNode(Node node)
  3. serializeNode(Node node, String filename)
  4. serializeNode(Node node, String indentLevel, StringWriter writer)
  5. serializeNode(Node xmlNode)
  6. serializeRDFToHTML(String xmlRDF)
  7. serializeSource(Source source)
  8. serializeToString(Node node, String encoding)
  9. serializeToXML(Node node, boolean indent)