Constructing Documents using Event Writers

In the previous section we showed you how to load a document into a container by reading that document from disk, or by providing the document as a string object. As an alternative, you can construct your document using an XmlEventWriter class object, which stores the document in an XmlDocument object. You can then put that XmlDocument object to the container as described in the previous section.

XmlEventWriter provides methods that allow you to describe the individual, descrete sections of the document. It is useful if, for example, you are already parsing a document using a SAX parser and you want to write the information your parser discovers to a container.

To use an event writer:

  1. Create the XmlDocument instance.

  2. Give it a name using the XmlDocument.setName() method.

  3. Put the document to your container using the XmlContainer.putDocumentAsEventWriter() method. Note that at this point you have not actually written any document data to the container, since your document is currently empty.

    This method returns an XmlEventWriter object.

  4. Use the XmlEventWriter object to start new document. You do this using the XmlEventWriter.writeStartDocument() method, which allows you to describe information about the XML document such as its encoding and it's XML version identification.

  5. Once you have started your document, you can write beginning and end elements, attributes, processing instructions, text, CDATA, and all the other features that you might expect to place on an XML document. XmlEventWriter provides methods that allow you to do these things.

  6. Once you have have completed your document, close it using the XmlEventWriter.close() method. This completes the container put operation that you began in step 3.

For example, suppose you wanted to write the following document to a container:

<a>
<b a1="one" b2="two">b node text</b>
<c>c node text</c>
</a> 

Then the following code fragment would accomplish that task:

         // Manager and container opens omitted for brevity.

         // create a new document
         XmlDocument doc = mgr.createDocument();
         doc.setName(dname);

         XmlEventWriter writer = 
            container.putDocumentAsEventWriter(doc);
         writer.writeStartDocument(null, null, null); // no XML decl

        // Write the document's root node. It has no prefixes or
        // attributes. This node is not empty.
        writer.writeStartElement("a", null, null, 0, false);

        // Write a new start element. This time for the "b" node.
        // It has two attributes and its content is also not empty.
        writer.writeStartElement("b", null, null, 2, false);
        // Write the "a1" and "b2" attributes on the "b" node
        writer.writeAttribute("a1", null, null, "one", true);
        writer.writeAttribute("b2", null, null, "two", true);
        // Write the "b" node's content. Note that there are 11
        // characters in this text, and we provide that information
        // to the method.
        writer.writeText(XmlManager.Characters, "b node text", 11);
        // End the "b" node
        writer.writeEndElement("b", null, null);
        // Start the "c" node. There are no attributes on this node.
        writer.writeStartElement("c", null, null, 0, false);
        // Write the "c" node's content
        writer.writeText(XmlManager.Characters, "c node text", 11);
        // End the "c" node and then the "a" (the root) node
        writer.writeEndElement("c", null, null);
        writer.writeEndElement("a", null, null);

        // End the document
        writer.writeEndDocument();
        // Close the document
        writer.close();