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:
Create the XmlDocument
instance.
Give it a name using the
XmlDocument::setName()
method.
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.
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.
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.
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:
// create a new document XmlDocument doc = mgr.createDocument(); doc.setName(dname); XmlEventWriter &writer = cont.putDocumentAsEventWriter(doc, uc); 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((const unsigned char *)"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((const unsigned char *)"b", NULL, NULL, 2, false); // Write the "a1" and "b2" attributes on the "b" node writer.writeAttribute((const unsigned char *)"a1", NULL, NULL, (const unsigned char *)"one", true); writer.writeAttribute((const unsigned char *)"b2", NULL, NULL, (const unsigned char *)"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. Also, we identify this data as being of type // XmlEventType.Characters. writer.writeText(Characters, (const unsigned char *)"b node text", 11); // End the "b" node writer.writeEndElement((const unsigned char *)"b", NULL, NULL); // Start the "c" node. There are no attributes on this node. writer.writeStartElement((const unsigned char *)"c", NULL, NULL, 0, false); // Write the "c" node's content writer.writeText(Characters, (const unsigned char *)"c node text", 11); // End the "c" node and then the "a" (the root) node writer.writeEndElement((const unsigned char *)"c", NULL, NULL); writer.writeEndElement((const unsigned char *)"a", NULL, NULL); // End the document writer.writeEndDocument(); // Close the document writer.close();