!--a11y-->
Transforming Without Stylesheet -
Converting 
To transform from one kind of source to some other kind of result you must get an instance of TransformerFactory and then use newTransformer() to get a transformer, which is not associated with a stylesheet:
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
Now you might, optionally, set some OutputProperties to the transformer:
transformer.setOutputProperty(OutputKeys.INDENT, “yes”);
And finally do a transformation:
...
1. Stream -> Stream (Changing the indent)
transformer.transform(new StreamSource(“source.xml”), new StreamResult(“Result.xml”);
2. Stream -> DOM (also known as DOM Parsing)
transformer.transform( new StreamSoruce(“source.xml”), new DOMResult(dom_node));
3. DOM -> SAX
transformer.transform( new DOMSource(dom_node), new SAXResult(sax_defaulthandler));
The following code fragment illustrates the above issues:
|
public class JAXPSourceConvertExample { public static void main(String[] args) throws Exception { String xml = "data/current.xml"; // Obtaining an instance of the factory TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); DOMResult domResult = new DOMResult();
// stream -> dom System.out.println("\nStream -> DOM "); transformer.transform(new StreamSource(xml), domResult);
// sax -> stream System.out.println("\nSAX -> Stream "); transformer.transform(new SAXSource(new InputSource(xml)), new StreamResult(System.out));
// dom -> stream System.out.println("\nDOM -> Stream"); transformer.transform(new DOMSource(domResult.getNode()), new StreamResult(System.out));
// stream -> sax System.out.println("\nStream -> SAX"); transformer.transform(new StreamSource(xml), new SAXResult((ContentHandler)new SAXTreeStructure()));
// sax -> dom domResult = new DOMResult(); System.out.println("\nSAX -> DOM"); transformer.transform(new SAXSource(new InputSource(xml)), domResult);
// dom -> sax System.out.println("\nDOM -> SAX"); transformer.transform(new DOMSource(domResult.getNode()), new SAXResult((ContentHandler)new SAXTreeStructure())); } } |
