java DOM xml 文件创建 - 输出文件中没有制表符或空格

发布于 2024-10-19 10:51:49 字数 1917 浏览 2 评论 0原文

我已经浏览了 stackoverflow 上的帖子,但似乎没有任何帮助。

这是有的:

            // 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);

这就是我得到的输出:

<?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>

没有制表符或没有空格。

由于java可能存在错误,我设置了缩进数,并激活了OutputKeys.INDENT。

还有其他想法吗?

编辑1(在adarshr修复之后):

我现在有空格。只有第一个卫星条目被放置在第一行,这是不应该的。

<?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>

编辑 2:

所以当前状态是我现在有空格,但在 XML 声明之后没有换行。我该如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

诗化ㄋ丶相逢 2024-10-26 10:51:49

尝试像这样设置缩进量:

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

try setting the indent amount like this:

transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
2024-10-26 10:51:49

我玩过 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);

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);
維他命╮ 2024-10-26 10:51:49

我以前也遇到过同样的问题。问题是加载的 TransformerFactory 或 Transformer 类的实现与 Java 的预期不同。

为了解决这个问题,我们还必须设置一个系统属性。我稍后会尽力帮您拿到。

编辑:试试这个

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

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");

¢蛋碎的人ぎ生 2024-10-26 10:51:49

我可以给你两条建议

第一
您可以使用 xsl 文件获得漂亮的输出

第二
我发现有趣的库 ode-utils-XXX.jar

你可以这样写

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文