!--a11y-->
Transforming Without Stylesheet -
Converting 
Use this procedure to transform one kind of a source to a different kind of a result.
...
1. Get an instance of TransformerFactory and then use the newTransformer().
//Classloader preset may be also needed in systems with their own class loading systems
TransformerFactory
factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
2. Set OutputProperties to the transformer.
transformer.setOutputProperty(OutputKeys.INDENT, “yes”);
3. Perform the transformation.
...
a. Stream ® Stream (changing the indent)
transformer.transform(new StreamSource(“source.xml”), new StreamResult(“Result.xml”);
b. Stream ® DOM (also known as DOM Parsing)
transformer.transform( new StreamSoruce(“source.xml”), new DOMResult(dom_node));
c. DOM ® SAX
transformer.transform( new DOMSource(dom_node), new SAXResult(sax_defaulthandler));
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())); } } |