Download presentation
Presentation is loading. Please wait.
Published byGervais Wells Modified over 9 years ago
1
1 JAXP & XSLT
2
Objectives 2 TrAX API Transforming XML Documents Workshops
3
TrAX API XML Transformation Transforms the XML document into another XML document as output, which fulfills some particular objective. There are two unique cases XML to XML The output is always an XML document. It is mostly used when you need to create XML pipelines from XML chain. The XML pipeline is created when XML transformations are connected to each other. The XML chain is the collection of XML documents which bind with each other. XML to Data The output is always a byte stream. Comes under this case because the HTML document contains a stream of bytes TrAX APIs Is an API, which provides a general transformation model for converting XML documents
4
TrAX API packages PackagesDescriptions javax.xml.transform - Implements the generic APIs for transformation instructions and executing the transformation, starting from source to destination. - The interfaces present in this package are ErrorListener, Result, Source, SourceLocator, Templates, URIResolver. - The classes present in this package are OutputKeys, Transformer, TransformerFactory javax.xml.transform.stream - Implements streams and URI specific transformation APIs. - The classes present in this package are StreamResult, StreamSource javax.xml.transform.dom - Implements DOM-related transformation APIs. - The interface present in this package is DOMLocator javax.xml.transform.sax - Implements SAX-related transformation APIs. - The interfaces present in this package are TemplateHandler, TransformerHandler
5
XSLT Stylesheets Is a language used for transforming XML documents into XHTML documents for Web pages. Uses XPath language to navigate between XML documents and XHTML documents. Defines the source documents, which will be matched with a predefined set of templates in transformation process. If XSLT gets the same document, it transforms the matching part of the source document into the output document. XML Book titleauthorprice XSLT html headbody h1h2p text... Serialization abc...
6
Transformer class Is used to transform a XML source tree into a result tree that is a well formed xml output document MethodsDescriptions transform - public abstract void transform(Source xmlSource, Result outputTarget) throws TranformerException - Is used to process the source tree into the result tree setParameter - public abstract void setParameter(String name, Object value) - Is used to add parameter for the transformation process. - The parameters help in transformation the document getParameter - public abstract Object getParameter(String name) - Is used to get a parameter, which explicitly set using the setParameter() or setParameters() method clearParameters - public abstract void clearParameters() - Is used to clear all parameters set with the setParameter method setURIResolver - public abstract void setURIResolver(URIResolver resolver) - Sets an object, which is used to resolve the URIs getURIResolver - public abstract URIResolver getURIResolver() - Gets an object, which is used to resolve the URIs
7
Transformer class (cont) MethodsDescriptions setOutputProperties - public abstract void setOutputProperties(Properties prop) throws IllegalArgumentException - Sets the output properties for transformation getOutputProperties - public abstract Properties getOutputProperties() - Gets a copy of the output properties for transformation. setOutputProperty - public abstract void setOutputProperties(String name, String value) throws IllegalArgumentException - Sets an output property, which makes effects for the transformation getOutputProperty - public abstract String getOutputProperties(String name) throws IllegalArgumentException - Gets an output property, which makes effects for the transformation setErrorListener - public abstract void setErrorListener(ErrorListener listener) throws IllegalArgumentException - Sets the error event listener for the transformation getErrorListener - public abstract ErrorListener getErrorListener() - Gets the error event listener for the transformation
8
TransformerFactory class MethodsDescriptions newInstance - public static TranformerFactory newInstance() throws TransformerFactoryConfigurationError - Gets a new instance of a TransformerFactory newTransformer - public abstract Transformer newTransformer(Source source) throws TransformerConfigurationException - Is used to convert the source object into a Transformer object. This object should not be used in multiple threads newTemplates - public abstract Templates newTemplates(Source source) throws TransformerConfigurationException - Is used to convert the source object into a complied representation of source template setURIResolver - public abstract void setURIResolver(URIResolver resolver) - Is used to set the object that is to be used by default throughout the transformation getURIResolver - public abstract URIResolver getURIResolver() - Is used to get the object that is to be used by default throughout the transformation
9
TransformerFactory class (cont) MethodsDescriptions getFeature - public abstract Boolean getFeature(String name) - Is used to search the value of a parameter setAttribute - public abstract void setAttribute(String name, Object value) throws IllegalArgumentException - Permits the user to set the values of specific attributes getAttribute - public abstract Object getAttribute(String name) throws IllegalArgumentException - Permits the user to get the values of specific attributes setErrorListener - public abstract void setErrorListener(ErrorListener listener) throws IllegalArgumentException - Is used to set the error event listener getErrorListener - public abstract ErrorListener getErrorListener() - Is used to get the error event listener
10
OutputKeys 1.public static void main(String[] args) throws Exception { 2.TransformerFactory tf = transformerFactory.newInstance();TransformerFactory 3.Transformer t = tf.newTransformer();Transformer 4.t.setOutputProperty(OutputKeys.INDENT, "yes"); 5.t.setOutputProperty ("{http://xml.apache.org/xslt}indent-amount", "4"); 6.t.setOutputProperty(OutputKeys.METHOD, "xml"); 7.Source source = new SAXSource(new SerializeSax(), new InputSource());Source 8.Result result = new StreamResult(new OutputStreamWriter(System.out));Result 9.t.transform(source, result); 10. } 10
11
Source & Result interface Source Interface Result Interface MethodsDescriptions setSystemId - public void setSystemId(String systemId) - Is used to set the system identifier for Source getSystemId - public String getSystemId() - Is used to get the system identifier, which previously set with setSystemId() MethodsDescriptions setSystemId - public void setSystemId(String systemId) - Is used to set the system identifier for Result class getSystemId - public String getSystemId() - Is used to get the system identifier, which previously set with setSystemId() method
12
Template interface The object, which implements the Template interface, is the runtime result of transformation instructions. Can be called from multiple threads for a particular instance. MethodsDescriptions newTransformer - public Transformer newTransformer() throws TransformerConfigurationException - Is used to create a new transformation object getOutputProperties - public Properties getOutputProperties () - Is used to get the static properties for xsl:output. The xsl:output is used to define the format of the output
13
Transforming XML documents
14
TransformerFactory To create transformer and Template objects, an instance of the TransformerFactory class is used. Features of TransformerFactory are The system property of TransformerFactory is always set as javax.xml.transform. TransformerFactory. The default property is used by the TransformerFactory class if the property is not set manually. The newInstance() method is used by TransformerFactory class to create instance. A TransformerFactory never perform multiple operations at a time A transformation always has a source and a result.
15
Transformer An instance of transformer class is used to change a source tree into a result tree in an XML document. The TransformerFactory.newTransformer() method is used to get the instances of Transformer class To process the XML data from other sources and write the outputs, the instance of Transformer class is used. The objects of Transformer class functions correctly during simultaneous execution by multiple threads. Some methods used by this class are clearParameters() and getURIResolver().
16
Example public class TraxEx { public static void main (String[] args) { if(args.length<=0){ System.out.println("Usage: java TraxEx xsltFile xmlFile SortedOrders.html"); return; } try { TransformerFactory tf = TransformerFactory.newInstance (); Transformer trans = tf.newTransformer (new StreamSource(args[0])); trans.transform (new StreamSource(args[1]), new StreamResult(new FileOutputStream(args[2]))); System.out.println("File SortedOrders.html created in current folder"); }catch(Exception e){ e.printStackTrace (); }
17
Example (cont) XSLT file Customer Item Quantity Price
18
Example (cont) XML file Chris Color TV 5 7500 David DVD Player 15 4000 Mitchell Washing Machine 8 10000
19
Example (cont)
20
WORKSHOP ACTIVITIES Building the console Java application using XSLT to transform the XML document combining with XSLT file to HTML file.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.