Java format xml using Transformer with CDATA part

gspotdesigner

New Member
I want to format string to xml this my code: \[code\]Source xmlInput = new StreamSource(new StringReader(input));StringWriter stringWriter = new StringWriter();StreamResult xmlOutput = new StreamResult(stringWriter);TransformerFactory transformerFactory = TransformerFactory.newInstance();Transformer transformer = transformerFactory.newTransformer();transformer.setOutputProperty(OutputKeys.INDENT, "yes");transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(4));transformer.transform(xmlInput, xmlOutput);\[/code\]This is my String\[code\] <a><attr name="a1">this is a test</attr><attr name="a2"><![CDATA[this is a test inside cdata part]]></attr></a>\[/code\]Output\[code\]<?xml version="1.0" encoding="UTF-8"?><a> <attr name="a1">this is a test</attr> <attr name="a2"><![CDATA[this is a test inside cdata part]]></attr></a>\[/code\]desired\[code\]<?xml version="1.0" encoding="UTF-8"?><a> <attr name="a1"> this is a test </attr> <attr name="a2"> <![CDATA[this is a test inside cdata part]]> </attr></a>\[/code\]I want each new tag will start in new line.
 
Back
Top