3
<?xml version="1.0" encoding="UTF-8"?><Request>

<Id>

here the first tag was written into file followed by xml version, i need that need to write in next line. like below

<?xml version="1.0" encoding="UTF-8"?>
 <Request>
  <Id>

i Have use below code to write the xml into file,please help

TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(new File(
                Constants.xmlLocation
                        + Constants.metaDataXmlFileName
                        + format.format(calendar.getTime()) + ".xml"));

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(
                "{http://xml.apache.org/xslt}indent-amount", "5");
        transformer.transform(source, result);
Cœur
  • 37,241
  • 25
  • 195
  • 267
jcrshankar
  • 1,165
  • 8
  • 25
  • 45
  • its already answered, check this post here: http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java – tokhi Sep 04 '13 at 07:08
  • Also this can help http://stackoverflow.com/questions/5142632/java-dom-xml-file-create-have-no-tabs-or-whitespaces-in-output-file?rq=1 – blackbird014 Sep 04 '13 at 07:08

1 Answers1

3

You can try the code below, this will work fine and you will be getting the formatted output

transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,"yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "10");
nimcap
  • 10,062
  • 15
  • 61
  • 69
Nicolas Rinaudo
  • 6,068
  • 28
  • 41