XML Transformation


TransformerFactory tFactory = TransformerFactory.newInstance();
SAXBuilder builder = new SAXBuilder();

Document myDocument =  builder.build(new File(“path to xml File”);

// Make the input sources for the XML and XSLT documents
org.jdom.output.DOMOutputter outputter = new org.jdom.output.DOMOutputter();
JDOMSource source = new JDOMSource(myDocument);
JDOMResult result = new JDOMResult();

File xsltFile = new File(“C:/dev/kd/xml/data.xsl”);

StreamSource xsltSource =
new StreamSource(new FileInputStream(xsltFile));

FileOutputStream out = new FileOutputStream(new File(“path to output”);
// Get a XSLT transformer
Transformer transformer = tFactory.newTransformer(xsltSource);

// Do the transform
transformer.transform(source, result);
Document outDoc = result.getDocument();
XMLOutputter outp = new XMLOutputter();

JDK javax.xml package:

TransformerFactory tFactory = TransformerFactory.newInstance();

File xsltFile = new File(“C:/dev/kd/xml/data.xsl”);

Templates template = tFactory.newTemplates(new StreamSource(
new FileInputStream(xsltFile)));
Transformer transformer = template.newTransformer();

// Do the transform
Source source = new StreamSource(new BufferedInputStream(new FileInputStream(new File(“Path to xml source”)));
Result result = new StreamResult(new FileOutputStream(new File(“path to output”));
Result printResult = new StreamResult(System.out);

transformer.transform(source, result);
source = new StreamSource(new FileInputStream(xmlFile));
transformer.setOutputProperty(OutputKeys.INDENT, “yes”);
transformer.transform(source, printResult);


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s