I have an XML document like this:
<document>
<root>
<field1>
<a
Type="anyType">
<b>Text</b>
</a>
<field1>
</root>
</document>
I want to remove the whitespace in the XML attribute Type
and the XML field a
The output should be like:
<document>
<root>
<field1>
<a Type="anyType">
<b>Text</b>
</a>
<field1>
</root>
</document>
How to do this in Java?
The code to generate the XML File is below:
DOMSource domSource = new DOMSource(document);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
String fileData = format(writer.toString());
// remove CR(carriage return)
fileData = fileData.replaceAll("&#[a-zA-Z0-9]{2};", "");
saveDocumentIntoFile(fileData, fileName);
method format()
public String format(String xml) {
try {
final InputSource src = new InputSource(new StringReader(xml));
final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
final Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml"));
final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
final LSSerializer writer = impl.createLSSerializer();
// Set this to true if the output needs to be beautified.
writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
// Set this to true if the declaration is needed to be outputted.
writer.getDomConfig().setParameter("xml-declaration", keepDeclaration);
return writer.writeToString(document);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
method saveDocumentIntoFile()
protected void saveDocumentIntoFile(String fileData, String fileName) throws IOException {
FileWriter batchFile = new FileWriter(fileName);
batchFile.write(fileData);
batchFile.close();
}