DOM serializer
import java.io.IOException;
import java.io.Writer;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class MainClass {
static private String indent = "";
public static Writer writer;
public static void serialize(Document doc) throws IOException {
serializeNode(doc, "");
writer.flush();
}
private static void serializeNode(Node node, String indentLevel) throws IOException {
switch (node.getNodeType()) {
case Node.DOCUMENT_NODE:
Document doc = (Document) node;
writer.write("<?xml version=\"");
writer.write(doc.getXmlVersion());
writer.write("\" encoding=\"UTF-8\" standalone=\"");
if (doc.getXmlStandalone())
writer.write("yes");
else
writer.write("no");
writer.write("\"?>\n");
NodeList nodes = node.getChildNodes();
if (nodes != null)
for (int i = 0; i < nodes.getLength(); i++)
serializeNode(nodes.item(i), "");
break;
case Node.ELEMENT_NODE:
String name = node.getNodeName();
writer.write(indentLevel + "<" + name);
NamedNodeMap attributes = node.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
Node current = attributes.item(i);
writer.write(" " + current.getNodeName() + "=\"");
print(current.getNodeValue());
writer.write("\"");
}
writer.write(">");
NodeList children = node.getChildNodes();
if (children != null) {
if ((children.item(0) != null) && (children.item(0).getNodeType() == Node.ELEMENT_NODE))
writer.write("\n");
for (int i = 0; i < children.getLength(); i++)
serializeNode(children.item(i), indentLevel + indent);
if ((children.item(0) != null)
&& (children.item(children.getLength() - 1).getNodeType() == Node.ELEMENT_NODE))
writer.write(indentLevel);
}
writer.write("</" + name + ">\n");
break;
case Node.TEXT_NODE:
print(node.getNodeValue());
break;
case Node.CDATA_SECTION_NODE:
writer.write("CDATA");
print(node.getNodeValue());
writer.write("");
break;
case Node.COMMENT_NODE:
writer.write(indentLevel + "<!-- " + node.getNodeValue() + " -->\n");
break;
case Node.PROCESSING_INSTRUCTION_NODE:
writer.write("<?" + node.getNodeName() + " " + node.getNodeValue() + "?>\n");
break;
case Node.ENTITY_REFERENCE_NODE:
writer.write("&" + node.getNodeName() + ";");
break;
case Node.DOCUMENT_TYPE_NODE:
DocumentType docType = (DocumentType) node;
String publicId = docType.getPublicId();
String systemId = docType.getSystemId();
String internalSubset = docType.getInternalSubset();
writer.write("<!DOCTYPE " + docType.getName());
if (publicId != null)
writer.write(" PUBLIC \"" + publicId + "\" ");
else
writer.write(" SYSTEM ");
writer.write("\"" + systemId + "\"");
if (internalSubset != null)
writer.write(" [" + internalSubset + "]");
writer.write(">\n");
break;
}
}
private static void print(String s) throws IOException {
if (s == null)
return;
for (int i = 0, len = s.length(); i < len; i++) {
char c = s.charAt(i);
switch (c) {
case '<':
writer.write("<");
break;
case '>':
writer.write(">");
break;
case '&':
writer.write("&");
break;
case '\r':
writer.write("
");
break;
default:
writer.write(c);
}
}
}
}
Related examples in the same category