views:

376

answers:

1

I'm writing an XML file with the following code:

Source source = new DOMSource(rootElement);
Result result = new StreamResult(xmlFile);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);

and this is the output file:

<?xml version="1.0" encoding="UTF-8"?>
<feature-sequences>
<sequence>
<initial-frame>0</initial-frame>
<points>
<point>
<x>274.0</x>
<y>316.0</y>
</point>
...

I want this file to be indented, for example:

<?xml version="1.0" encoding="UTF-8"?>
<feature-sequences>
  <sequence>
    <initial-frame>0</initial-frame>
    <points>
      <point>
        <x>274.0</x>
        <y>316.0</y>
      </point>
...

the call to setOutputProperty in my code doesn't solve the problem, it actually makes the text with new lines (but not indented).

anyone has a solution to this, without the need of external libraries?

+4  A: 

You may have to specify the amount of spaces to indent as well:

transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
Boris Terzic
thank you very much, that's exactly what I was looking for! but I think this property name is very weird.
cd1
Glad it worked! The only reason I know about it is because I got bit by this myself. XML related APIs in Java are horrible in general.
Boris Terzic