[Solved] splitting one line string into multiple lines


Another option is to parse the XML, and use the OutputKeys.INDENT option of the Transformer class to output the XML formatted.

The following example

Source source = new StreamSource(new StringReader(s));

TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", 4);

Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");

StreamResult result = new StreamResult(new StringWriter());
transformer.transform(source, result);

String xmlOutput = result.getWriter().toString();
System.out.println(xmlOutput);

String xmlOutput = result.getWriter().toString();
System.out.println(xmlOutput);

produces the output below

<?xml version="1.0" encoding="UTF-8"?>
<Item>
    <productname>COOLER MASTER Hyper 212 EVO RR-212E-20PK-R2</productname>
    <Price>$33.99</Price>
    <ItemID>1000</ItemID>
</Item>

solved splitting one line string into multiple lines