Formatting XML output using apache commons XMLConfiguration

I am using the XMLConfiguration apache configuration to create and save an XML file. When saving formatting, no. I get something like:

<root> <node> <child> </child> </node> </root> 

I know that there are many ways to use some other library to get this output and formatting, but, of course, should there be a way to set something simple, like indenting from the community configuration?

+6
source share
2 answers

The same problem was detected. Although the question was asked a long time ago, I would like to share a solution:

The XMLConfiguration class has a protected method called createTransformed. It must be expanded and set to the correct indentation configuration .

 public class ExtendedXMLConfiguration extends XMLConfiguration { public ExtendedXMLConfiguration(File file) throws ConfigurationException { super(file); } @Override protected Transformer createTransformer() throws TransformerException { Transformer transformer = super.createTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); return transformer; } } 
+9
source

You can refer to these topics , which provides a lot of easy way to process the XML format in Java.

-2
source

Source: https://habr.com/ru/post/886515/


All Articles