Methods Summary |
---|
public void | serialize(org.w3c.dom.Document doc, java.io.OutputStream out) This serializes a DOM tree to the supplied
OutputStream .
Writer writer = new OutputStreamWriter(out);
serialize(doc, writer);
|
public void | serialize(org.w3c.dom.Document doc, java.io.File file) This serializes a DOM tree to the supplied
OutputStream .
Writer writer = new FileWriter(file);
serialize(doc, writer);
|
public void | serialize(org.w3c.dom.Document doc, java.io.Writer writer) This serializes a DOM tree to the supplied
OutputStream .
// Start serialization recursion with no indenting
serializeNode(doc, writer, "");
writer.flush();
|
public void | serializeNode(org.w3c.dom.Node node, java.io.Writer writer, java.lang.String indentLevel) This will serialize a DOM Node to
the supplied Writer .
// Determine action based on node type
switch (node.getNodeType()) {
case Node.DOCUMENT_NODE:
writer.write("<?xml version=\"1.0\"?>");
writer.write(lineSeparator);
// recurse on each child
NodeList nodes = node.getChildNodes();
if (nodes != null) {
for (int i=0; i<nodes.getLength(); i++) {
serializeNode(nodes.item(i), writer, "");
}
}
/*
Document doc = (Document)node;
serializeNode(doc.getDocumentElement(), writer, "");
*/
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() +
"=\"" + current.getNodeValue() +
"\"");
}
writer.write(">");
// recurse on each child
NodeList children = node.getChildNodes();
if (children != null) {
if ((children.item(0) != null) &&
(children.item(0).getNodeType() ==
Node.ELEMENT_NODE)) {
writer.write(lineSeparator);
}
for (int i=0; i<children.getLength(); i++) {
serializeNode(children.item(i), writer,
indentLevel + indent);
}
if ((children.item(0) != null) &&
(children.item(children.getLength()-1)
.getNodeType() ==
Node.ELEMENT_NODE)) {
writer.write(indentLevel);
}
}
writer.write("</" + name + ">");
writer.write(lineSeparator);
break;
case Node.TEXT_NODE:
writer.write(node.getNodeValue());
break;
case Node.CDATA_SECTION_NODE:
writer.write("<![CDATA[" +
node.getNodeValue() + "]]>");
break;
case Node.COMMENT_NODE:
writer.write(indentLevel + "<!-- " +
node.getNodeValue() + " -->");
writer.write(lineSeparator);
break;
case Node.PROCESSING_INSTRUCTION_NODE:
writer.write("<?" + node.getNodeName() +
" " + node.getNodeValue() +
"?>");
writer.write(lineSeparator);
break;
case Node.ENTITY_REFERENCE_NODE:
writer.write("&" + node.getNodeName() + ";");
break;
case Node.DOCUMENT_TYPE_NODE:
DocumentType docType = (DocumentType)node;
writer.write("<!DOCTYPE " + docType.getName());
if (docType.getPublicId() != null) {
System.out.print(" PUBLIC \"" +
docType.getPublicId() + "\" ");
} else {
writer.write(" SYSTEM ");
}
writer.write("\"" + docType.getSystemId() + "\">");
writer.write(lineSeparator);
break;
}
|
public void | setIndent(java.lang.String indent) This sets the indentation to use.
this.indent = indent;
|
public void | setLineSeparator(java.lang.String lineSeparator) This sets the line separator to use.
this.lineSeparator = lineSeparator;
|