0

I have a java code which writes the output to an XML by using DOM parsing strategy. The contents of the XML after writing it are as follows

<L-1><?xm-replace_text {L-1}?></L-1>

Now if I rewrite the same XML after making some changes, the contents of XML after writing will be as follows

<L-1>
                                  <?xm-replace_text {L-1}?>
                                </L-1>

I am using the same transformer every time I write the XML as

        encoding = (doc.getXmlEncoding() != null) ? doc.getXmlEncoding()
                : "iso-8859-1";
        version = (doc.getXmlVersion() != null) ? doc.getXmlVersion()
                : "1.0";

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
transformer.setOutputProperty(OutputKeys.VERSION, version);

Why do I have the indentation problem ?

Vishnukk
  • 524
  • 2
  • 11
  • 27

1 Answers1

0

You should have specified the amount of indentation.

Example:

transformer.setOutputProperty( OutputKeys.INDENT, "yes" );  
String propertyFormat1 = "b;http://xml.apache.org/xsltd;indent-amount";
String propertyFormat2 = "{http://xml.apache.org/xslt}indent-amount";

// this  
// transformer.setOutputProperty( propertyFormat1 , "2" );  
// or, the following  
transformer.setOutputProperty( propertyFormat2, "2" );  

Refer to some Examples:

  1. FormattinganXMLfileusingTransformer
  2. Java: How to Indent XML Generated by Transformer (On SO)
Community
  • 1
  • 1
Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82