Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform XML style checking?

Unformatted XML is a problem in our build. I would like to include style checking for XML documents, primarily looking for poor indentation.

What is the best tool to use? (The build uses Ant).

like image 644
Synesso Avatar asked Jun 30 '26 06:06

Synesso


1 Answers

You can write a class that will automatically transform and therefore indent your XML. Then just specify in Ant on which XML files it should be ran.

Something to get you started:

String filePath = "test.xml";
String outputFilePath = "testOut.xml";

File xmlFile = new File(filePath);
File outputXmlFile = new File(outputFilePath);

Transformer transformer = TransformerFactory.newInstance().newTransformer();

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

StreamSource ss = new StreamSource(xmlFile);
StreamResult sr = new StreamResult(outputXmlFile);      

transformer.transform(ss, sr);
like image 111
darioo Avatar answered Jul 01 '26 19:07

darioo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!