0

I am using JAXP to convert DOM tree to XML. I do not want any intends in my result XML. This is my code:

    root.normalize();
    DOMSource domSource = new DOMSource(root);
    StreamResult result = new StreamResult(outputStream);
    
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();

    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    
    transformer.transform(domSource, result);

It works nice if source is not intended and result is also not intended, If the source XML file is pretty formatted, then "INDENT = no" takes no effect. The transformed XML file is still indented but I do not want that.

This input generate correct output with no intends.:

<InitMessage xmlns="http://www.test.com/"><operation>while</operation><part1>6</part1><part2>2</part2><part3>5</part3><part4>1</part4></InitMessage>

But this one no and I still got pretty printed intended xml in my output (one line).

<InitMessage xmlns="http://www.test.com/">
    <operation>while</operation>
    <part1>6</part1>
    <part2>2</part2>
    <part3>5</part3>
    <part4>1</part4>
</InitMessage>
TNN
  • 426
  • 2
  • 6
  • 15

1 Answers1

0

Setting INDENT to no only indicates that the processor is not allowed to add additional whitespace, not that it will strip existing whitespace.

jottler189
  • 58
  • 4