Presentation is loading. Please wait.

Presentation is loading. Please wait.

17 Apr 2002 XML Programming: TrAX Andy Clark. Java API for XML Processing Standard Java API for loading, creating, accessing, and transforming XML documents.

Similar presentations


Presentation on theme: "17 Apr 2002 XML Programming: TrAX Andy Clark. Java API for XML Processing Standard Java API for loading, creating, accessing, and transforming XML documents."— Presentation transcript:

1 17 Apr 2002 XML Programming: TrAX Andy Clark

2 Java API for XML Processing Standard Java API for loading, creating, accessing, and transforming XML documents – JAXP: parsing Supports DOM and SAX – TrAX: transformation Generic transformation framework Java standard extension (i.e. javax package) – Defined by Sun JCP

3 Transformation API for XML Transformation framework – Allows generic transformation from one form to another Examples: – Serializers – XSLT processors – etc…

4 javax.xml.transform (1 of 2) Transformation – Transformer, TransformerFactory – Templates Input and Output – Source – Result

5 javax.xml.transform (2 of 2) Transformer control – ErrorListener – SourceLocator, URIResolver – OutputKeys Other – TransformerConfigurationException – TransformerException – TransformerFactoryConfigurationError

6 Input and Output javax.xml.transform.stream – StreamSource – StreamResult javax.xml.transform.dom – DOMSource, DOMResult – DOMLocator javax.xml.transform.sax – SAXSource, SAXResult – SAXTransformerFactory – TransformerHandler, TemplatesHandler

7 Stream Input and Output StreamSource – Specifies document stream to parse StreamResult – Specifies stream for serialization of document – OutputKeys controls serialization settings

8 DOM/SAX Input and Output Document Object Model – DOMSource Specifies document or node to process – DOMResult Specifies document factory for new nodes Simple API for XML – SAXSource Specifies XMLReader to parse document – SAXResult Specifies handler for generated events

9 Transform Document (1 of 3) Identity transform – Get instance of transformer factory TransformerFactory factory = TransformerFactory.newInstance(); – Create transformer Transformer transformer = factory.newTransformer(); – Create input and output Source source = new StreamSource(“document.xml”); Result result = new StreamResult(System.out); – Transform document transformer.transform(source, result);

10 Transform Document (2 of 3) Transform with stylesheet (1 of 2) – Get instance of transformer factory TransformerFactory factory = TransformerFactory.newInstance(); – Create templates Source templatesSource = new StreamSource(“style.xsl”); Templates templates = factory.newTemplates(templatesSource); – Create transformer Transformer transformer = templates.newTransformer();

11 Transform Document (3 of 3) Transform with Stylesheet (2 of 2) – Create input and output Source source = new StreamSource(“document.xml”); Result result = new StreamResult(System.out); – Transform document transformer.transform(source, result);

12 Error Handling and Entity Resolution Transformer – Can set a TrAX ErrorListener – Can set a TrAX URIResolver URIResolver – Called when reference to multiple documents Passed base and literal system identifiers of resource Example XPath: “document(‘book.xml’)/book/title” – Returns a TrAX Source Note: This is super cool because it means that you can cache (or generate) documents in memory.

13 Useful Links JAXP – http://java.sun.com/xml/ Apache Xalan XSLT Processor – http://xml.apache.org/xalan-j/

14 XML Programming: TrAX Andy Clark


Download ppt "17 Apr 2002 XML Programming: TrAX Andy Clark. Java API for XML Processing Standard Java API for loading, creating, accessing, and transforming XML documents."

Similar presentations


Ads by Google