Using JAXP to Transform XML Data Example of Transforming an XML Document Using JAXP

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

Transformation refers to converting an XML document the source of the transformation into another format, typically a different XML document, HTML, Wireless Markup Language WML the result of the transformation. This section describes how to transform XML documents using JAXP and from within a JSP using JSP tags.

3.4.1 Using JAXP to Transform XML Data

Version 1.2 of JAXP provides pluggable transformation, which means that you can use any JAXP-compliant transformer engine. JAXP provides the following interfaces to transform XML data into a variety of formats: ■ javax.xml.transform Contains the generic APIs for transforming documents. This package does not have any dependencies on SAX or DOM and makes the fewest possible assumptions about the format of the source and result. ■ javax.xml.transform.stream Implements stream- and URI-specific transformation APIs. In particular, it defines the StreamSource and StreamResult classes that enable you to specify InputStreams and URLs as the source of a transformation and OutputStreams and URLs as the results, respectively. ■ javax.xml.transform.dom Implements DOM-specific transformation APIs. In particular, it defines the DOMSource and DOMResult classes that enable you to specify a DOM tree as either the source or result, or both, of a transformation. ■ javax.xml.transform.sax Implements SAX-specific transformation APIs. In particular, it defines the SAXSource and SAXResult classes that enable you to specify org.xml.sax.ContentHandler events as either the source or result, or both, of a transformation. Transformation encompasses many possible combinations of inputs and outputs.

3.4.2 Example of Transforming an XML Document Using JAXP

The following example snippet shows how to use JAXP to transform myXMLdoc.xml into a different XML document using the mystylesheet.xsl stylesheet: import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; Developing XML Applications with WebLogic Server 3-9 import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamResult; Transformer trans; TransformerFactory factory = TransformerFactory.newInstance; String stylesheet = file:stylesheetsmystylesheet.xsl; String xml_doc = file:xml_docsmyXMLdoc.xml; trans = factory.newTransformernew StreamSourcestylesheet; trans.transformnew StreamSourcexml_doc, new StreamResultSystem.out; For an example of how to transform a DOM document into an XML stream, see Section 3.4.1, Using JAXP to Transform XML Data.

3.4.3 Using the JSP Tag to Transform XML Data