So I am using the DOM parser in JAVA to create an XML file.
When I want to add a child element it ends up looking like this:
<childElement attribute1="attributeValue" attribute2="attributeValue"/>
However I would rather that my XML look like this (though obviously DOM won’t make these indentations and seperate lines):
<childElement>
<attribute1>attributeValue</attribute1>
<attribute2>attributeValue</attribute2>
</childElement>
Existing Code that was outputed (what i don’t want):
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Dennis>
<Hair Colour="Brown"/>
</Dennis>
<!--This is a comment :)-->
What I want it to look like:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Dennis>
<Hair>
<Colour>Brown</colour>
</Hair>
</Dennis>
<!--This is a comment :)-->
*Note: I understand if java won’t be able to make all these indentations and what not. What I DO want it to do is to use the "<open tag>value<close tag>" format.
Simply add an element named
attribute1to the elementchildElement.DOM itself is memory based but you can serialize it to xml and most libraries have pretty printers, that do the indentation.
Further Reading
The section Write document to file shows how to format the output with this library.