3

I already looked through the postings on stackoverflow but it seems that nothing helps.

Here is what have:

            // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", 2);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        DOMSource source = new DOMSource(xmlDoc);
        StreamResult result =  new StreamResult(new File("C:\\testing.xml"));
        transformer.transform(source, result);

and this is what I get as output:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Satellite SatelliteName="" XmlFileVersion="">
<test0>
<test1>
<test2>
<test3>
<test4>
<test5>
<test6>
<test7>
<test8>
<test9/>
</test8>
</test7>
</test6>
</test5>
</test4>
</test3>
</test2>
</test1>
</test0>
</Satellite>

No tabs or no spaces.

I set the indent-number because of a possible bug of java and I activated OutputKeys.INDENT.

Any other ideas?

Edit 1 (after adarshr's fix):

I now have white spaces. Only the first Satellite Entry is placed in the first line which shouldn't be.

<?xml version="1.0" encoding="UTF-8"?><Satellite SatelliteName="" XmlFileVersion="">
  <test0>
    <test1>
      <test2>
        <test3>
          <test4>
            <test5>
              <test6>
                <test7>
                  <test8>
                    <test9>blah</test9>
                  </test8>
                </test7>
              </test6>
            </test5>
          </test4>
        </test3>
      </test2>
    </test1>
  </test0>
  <sdjklhewlkr/>
</Satellite>

Edit 2:

So the current state is that I now have whitespaces but I have no line feed after the XML declaration. How can I fix this?

1
  • I think you are stuck with the first line problem, the xml encoding tag doesn't seem to be treated as a real node in any parsing/formatting I've ever done, so it looks just like yours. Commented Feb 28, 2011 at 16:18

4 Answers 4

4

try setting the indent amount like this:

transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
Sign up to request clarification or add additional context in comments.

3 Comments

I get the same output as before.
@fpdragon - are you including an external xalan impl or not? i'd recommend trying it with the jdk builtin implementation.
I tried it without the xalan but it throws an exception. After this I noticed that the apache url is in the command so I expect this command only works with xalan?!
2

I've played with Transformer, but never got it to work. I used the Xerces (Apache) library, which has always worked like a charm for me. Try something like

OutputFormat format = new OutputFormat(document);
format.setLineWidth(65);
format.setIndenting(true);
format.setIndent(2);
Writer outxml = new FileWriter(new File("out.xml"));
XMLSerializer serializer = new XMLSerializer(outxml, format);
serializer.serialize(document);

Comments

1

I had faced the same problem sometime back. The issue was that the implementation of the TransformerFactory or Transformer classes loaded was different from what Java intends it to be.

There was also a System property that we had to set in order to solve it. I will try and get that for you in a moment.

EDIT: Try this

System.setProperty("javax.xml.transform.TransformerFactory", "org.apache.xalan.xsltc.trax.TransformerFactoryImpl");

5 Comments

I got the following exception after this change: Exception in thread "AWT-EventQueue-0" javax.xml.transform.TransformerFactoryConfigurationError: Provider org.apache.xalan.xsltc.trax.TransformerFactoryImpl not found
Obviously, you need to have Apache Xalan JAR file in the classpath! xml.apache.org/xalan-j
Hmmm so there is no out of the box solution?
Ok tried this and it works except one little thing. Please see above.
That's good and also a little troublesome to hear that. I'll see if I can find a fix for this for you.
0

I can give you 2 advice

1st You can use xsl file for pretty output

2nd I found interesting library ode-utils-XXX.jar

And you can just write like

String result = "";
        try {
            result = DOMUtils.prettyPrint(doc);
        } catch (IOException e) {           
            e.printStackTrace();
        }
        System.out.println(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.