Java DOM xml file create - no tabs or spaces in the output file

I already looked at publications in stackoverflow, but it seems that nothing helps.

Here is what it is:

            // 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 spaces.

I set the indent number due to a possible java error and I activated OutputKeys.INDENT.

Any other ideas?

Change 1 (after fixing adarshr):

Now I have spaces. Only the first satellite entry is placed on the first line, which should not 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 now I have spaces, but after the XML declaration, I don't get the string. How can i fix this?

+3
source share
4 answers

try setting the indent size as follows:

transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
+4

Transformer, . Xerces (Apache), . -

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);
+2

I faced the same problem sometime. The problem was that the implementation of the TransformerFactory or Transformer loadable classes was different from what Java intended to do.

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

EDIT : try it

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

+1
source

I can give you 2 tips

first you can use xsl file for quite output

second I found an interesting library ode-utils-XXX.jar

And you can just write how

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

Source: https://habr.com/ru/post/1795221/


All Articles