Getting the XMLStreamWriter Object Adding the XML Declaration to the Output Stream Adding Standard XML Events to the Output Stream

4-16 Programming XML for Oracle WebLogic Server Instantiate a writer XMLStreamWriter xmlw = xmlof.createXMLStreamWriternew FileOutputStream outFile.xml; System.out.printlnREADER: + xmlw + \n; Generate the XML Write the default XML declaration xmlw.writeStartDocument; xmlw.writeCharacters\n; xmlw.writeCharacters\n; Write a comment xmlw.writeCommentthis is a comment; xmlw.writeCharacters\n; Write the root element person with a single attribute gender xmlw.writeStartElementperson; xmlw.writeNamespaceone, http:namespaceOne; xmlw.writeAttributegender,f; xmlw.writeCharacters\n; Write the name element with some content and two attributes xmlw.writeCharacters ; xmlw.writeStartElementone, name, http:namespaceOne; xmlw.writeAttributehair,pigtails; xmlw.writeAttributefreckles,yes; xmlw.writeCharactersPippi Longstocking; End the name element xmlw.writeEndElement; xmlw.writeCharacters\n; End the person element xmlw.writeEndElement; End the XML document xmlw.writeEndDocument; Close the XMLStreamWriter to free up resources xmlw.close; } }

4.3.2 Getting the XMLStreamWriter Object

Use the XMLOutputFactory.createXMLStreamWriter method to instantiate an XMLStreamWriter object based on an XML document, as shown in the following code excerpt: XMLStreamWriter xmlw = xmlof.createXMLStreamWriternew FileOutputStream outFile.xml; In the example, xmlof is the XMLOutputFactory instance. Using the Streaming API for XML StAX 4-17 The various signatures of the createXMLStreamWriter method allow for the following XML document formats as parameters: ■ java.io.OutputStream shown in the example ■ java.io.Writer ■ javax.xml.transform.Result specified in the JAXP API http:java.sun.comxml

4.3.3 Adding the XML Declaration to the Output Stream

Use the XMLStreamWriter.writeStartDocument method to add the XML declaration as the first line of the XML document, as shown in the following code excerpt: xmlw.writeStartDocument; With no arguments, the method writes the default XML declaration: ?xml version=1.0 encoding=utf-8? If you want to specify a different encoding or XML version, use the following flavors of the writeStartDocument method: ■ writeStartDocumentjava.lang.String version ■ writeStartDocumentjava.lang.String encoding, java.lang.String version Setting the encoding with the writeStartDocument method does not set the actual encoding of the underlying output; it simply specifies what value is written for the encoding attribute of the XML declaration. To actually set the encoding of the output, you must specify the encoding parameter when creating the instance of the XMLStreamWriter with the appropriate XMLOutputFactory.createXMLStreamWriter method.

4.3.4 Adding Standard XML Events to the Output Stream

Use the XMLStreamWriter.writeXXX methods to add standard XML events, such as start elements, end elements, comments, CDATA, entity references, and so on to the output stream.The XXX refers to the particular event, such as writeStartElement, writeEndElement, writeComment, writeCData, and so on. You can create most elements by passing the name or text data as a String. The XMLStreamWriter interface does not validate your data, nor does it check that the document is well-formed; it is the programmers responsibility to ensure that, for example, each start element has a corresponding end element, and so on. It is also up to the programmer to ensure that the start and end element events are correctly nested. To make the output XML more human-readable when writing to a text file, use the writeCharacters\n method to add new lines in appropriate places. For example, assume you want to create the following snippet of XML: -- This is a comment -- nameJane Doename The Java code to add this element to an output stream is as follows: xmlw.writeCommentThis is a comment; xmlw.writeCharacters\n; 4-18 Programming XML for Oracle WebLogic Server xmlw.writeStartElementname; xmlw.writeCharactersJane Doe; xmlw.writeEndElement; xmlw.writeCharacters\n;

4.3.5 Adding Attributes and Namespace Declarations to a Start Element