Java DOM XML file creation – there are no tabs or spaces in the output file
•
Java
I've checked the post on stack overflow, but it doesn't seem to help
What is this:
// write the content into xml file TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number",2); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT,"yes"); DOMSource source = new DOMSource(xmlDoc); StreamResult result = new StreamResult(new File("C:\\testing.xml")); transformer.transform(source,result);
This is the output I get:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <Satellite SatelliteName="" XmlFiLeversion=""> <test0> <test1> <test2> <test3> <test4> <test5> <test6> <test7> <test8> <test9/> </test8> </test7> </test6> </test5> </test4> </test3> </test2> </test1> </test0> </Satellite>
No labels or spaces
I set the indent number because there may be an error in Java, and I activated outputkeys INDENT.
Any other ideas?
Edit 1 (after adarshr repair):
I have a space now Only the first satellite entry is in the first row and should not be in the first row
<?xml version="1.0" encoding="UTF-8"?><Satellite SatelliteName="" XmlFiLeversion=""> <test0> <test1> <test2> <test3> <test4> <test5> <test6> <test7> <test8> <test9>blah</test9> </test8> </test7> </test6> </test5> </test4> </test3> </test2> </test1> </test0> <sdjklhewlkr/> </Satellite>
Edit 2:
So the current state is that I now have spaces, but I don't have line breaks after the XML declaration How can I solve this problem?
Solution
Try to set the indent amount as follows:
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","2");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码