java DOM xml 文件创建 - 输出文件中没有制表符或空格
我已经浏览了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试像这样设置缩进量:
try setting the indent amount like this:
我玩过 Transformer,但从未让它工作过。我使用了 Xerces (Apache) 库,它对我来说一直很有吸引力。尝试类似的东西
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
我以前也遇到过同样的问题。问题是加载的 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");
我可以给你两条建议
第一
您可以使用 xsl 文件获得漂亮的输出
第二
我发现有趣的库 ode-utils-XXX.jar
你可以这样写
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