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).
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With