FileDocCategorySizeDatePackage
DOMSerializer.javaAPI DocExample8943Thu Aug 16 16:50:08 BST 2001javaxml2

DOMSerializer

public class DOMSerializer extends Object
DOMSerializer will take a DOM tree and serialize that tree.

Fields Summary
private String
indent
Indentation to use
private String
lineSeparator
Line separator to use
Constructors Summary
public DOMSerializer()

This initializes the needed settings.

        indent = "";
        lineSeparator = "\n";
    
Methods Summary
public voidserialize(org.w3c.dom.Document doc, java.io.OutputStream out)

This serializes a DOM tree to the supplied OutputStream.

param
doc DOM tree to serialize.
param
out OutputStream to write to.

        
        Writer writer = new OutputStreamWriter(out);
        serialize(doc, writer);
    
public voidserialize(org.w3c.dom.Document doc, java.io.File file)

This serializes a DOM tree to the supplied OutputStream.

param
doc DOM tree to serialize.
param
file File to write to.


        Writer writer = new FileWriter(file);
        serialize(doc, writer);
    
public voidserialize(org.w3c.dom.Document doc, java.io.Writer writer)

This serializes a DOM tree to the supplied OutputStream.

param
doc DOM tree to serialize.
param
writer Writer to write to.


        // Start serialization recursion with no indenting
        serializeNode(doc, writer, "");
        writer.flush();
    
public voidserializeNode(org.w3c.dom.Node node, java.io.Writer writer, java.lang.String indentLevel)

This will serialize a DOM Node to the supplied Writer.

param
node DOM Node to serialize.
param
writer Writer to write to.
param
indentLevel current indentation.


        // 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 voidsetIndent(java.lang.String indent)

This sets the indentation to use.

param
indent the indentation String to use.

        this.indent = indent;
    
public voidsetLineSeparator(java.lang.String lineSeparator)

This sets the line separator to use.

param
lineSeparator line separator to use.

        this.lineSeparator = lineSeparator;