Generating XML from a DOM Document Tree Generating XML Documents in a JSP

Developing XML Applications with WebLogic Server 3-7

3.3.1 Generating XML from a DOM Document Tree

You can use the javax.xml.transform.Transformer class to serialize a DOM object into an XML stream, as shown in the following example segment: import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.; ... TransformerFactory trans_factory = TransformerFactory.newInstance; Transformer xml_out = trans_factory.newTransformer; Properties props = new Properties; props.putmethod, xml; xml_out.setOutputPropertiesprops; xml_out.transformnew DOMSourcedoc, new StreamResultSystem.out; In the example, the Transformer.transform method does the work of converting a DOM object into an XML stream. The transform method takes as input a javax.xml.transform.dom.DOMSource object, created from the DOM tree stored in the doc variable, and converts it into a javax.xml.transform.stream.StreamResult object and writes the resulting XML document to the standard output.

3.3.2 Generating XML Documents in a JSP

You typically use JSPs to generate HTML, but you can also use a JSP to generate an XML document. Using JSPs to generate XML requires that you set the content type of the JSP page as follows: page contentType=textxml ... XML document The following code shows an example of how to use JSP to generate an XML document: ?xml version=1.0 page contentType=textxml import=java.text.DateFormat,java.util.Date message text Hello World. text Note: For detailed instructions on using the Streaming API for XML StAX to generate XML documents, see Chapter 4, Using the Streaming API for XML StAX. 3-8 Programming XML for Oracle WebLogic Server timestamp out.printDateFormat.getDateInstance.formatnew Date; timestamp message For more information about using JSP to generate XML, see http:java.sun.comproductsjsphtmlJSPXML.html .

3.4 Transforming XML Documents