Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force java xml dom to produce newline after <?xml version="1.0" encoding="UTF-8"?>?

I use this code to enable newlines:

    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

But I get following output:

<?xml version="1.0" encoding="UTF-8"?><root>
  <child>aaaa</child>
</root>

I'd like to have newline before root element. What should I do?

like image 854
Stepan Yakovenko Avatar asked Jun 30 '12 17:06

Stepan Yakovenko


People also ask

Which encoding by default set in XML?

XML Encodings xml version="1.0" encoding="ISO-8859-1"?> Without this information, the default encoding is UTF-8 or UTF-16, depending on the presence of a UNICODE byte-order mark (BOM) at the beginning of the XML file.

Is java and XML same?

Yes. Android uses xml to declare layouts and java to provide logic. Note that while both activity_main and MainActivity follow common naming conventions, there is no need for them to be called this way.


2 Answers

Other answers did not work for me, or would be unacceptable in my application. This worked for me:

transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
like image 153
neuralmer Avatar answered Sep 18 '22 11:09

neuralmer


Another solution is : transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,"yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "10");

like image 44
Vikas Chowdhury Avatar answered Sep 21 '22 11:09

Vikas Chowdhury