问题描述
我试图在没有任何外部库的情况下对XML文件进行漂亮打印,但无法让Java做我想要的...这是我的代码到目前为止(我添加了类似问题的任何解决方案!):
I'm trying to prettyprint an XML-file without any external libraries, but cannot get Java to do what I want... Here is my code so far (I added any solutions from similar questions!):
TransformerFactory tfactory = TransformerFactory.newInstance();
tfactory.setAttribute("indent-number", 4);
Transformer transformer = tfactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "4");
transformer.setOutputProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "4");
File file = new File("C:\\text.xml");
DOMSource source = new DOMSource(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file));
transformer.transform(source, new StreamResult(file));
输入文件如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><root><test><item0>a</item0><item1>b</item1></test></root>
我收到一个如下所示的输出:
I receive an output looking like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<test>
<item0>a</item0>
<item1>b</item1>
</test>
</root>
现在我没有得到的是为什么在$ code之后有一个空行>< root> 和< / test>
之间的缩进,但无其他地方。在这个新文件上运行代码不会改变!
Now the thing I don't get is why there is a blank line after <root>
and an indentation before </test>
, but nowhere else. Running the code on this new file changes nothing!
我希望我的输出文件如下所示:
I would like my output file to look like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<test>
<item0>a</item0>
<item1>b</item1>
</test>
</root>
更新
我已经从我的代码中删除了一些显然没有做任何事情的行:
Update
I have removed some lines from my code that apparently weren't doing anything anyway:
TransformerFactory tfactory = TransformerFactory.newInstance();
tfactory.setAttribute("indent-number", 4);
Transformer transformer = tfactory.newTransformer();
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "4");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
File file = new File("C:\\text.xml");
DOMSource source = new DOMSource(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file));
transformer.transform(source, new StreamResult(file));
现在,从原始问题的单行文件创建一个漂亮的打印文件,问题的一部分回答了!我不知道究竟是什么问题,但无论如何,现在工作^^
This now creates a pretty printed file from the one-line-file from the original question, so that part of the question is answered! I don't know what exactly the problem was, but whatever, works now^^
但是我也有一些从我的程序读取和写入的旧文件看起来像这样:
But I also have some old files that are read from and written to by my program that look kind of like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<test>
<item0>a</item0>
<item1>b</item1>
</test>
</root>
它们在每个节点之后都有一个换行符,但没有缩进。我的代码保留文件不变...如何更正?
They have a line break after every node, but no indentation. My code leaves the file unchanged... How do I correct this?
推荐答案
你已经标记了这个XSLT,如果你申请以下XSLT样式表:
You have tagged this XSLT, and if you apply the following XSLT stylesheet:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xalan"
exclude-result-prefixes="xalan">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" xalan:indent-amount="4"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
到您的XML输入,结果将是:
to your XML input, the result will be:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<test>
<item0>a</item0>
<item1>b</item1>
</test>
</root>
现场演示:
请注意,项目漂亮打印为:
Note that the items are "pretty printed" as:
<item0>a</item0>
而不是显示在你的帖子中:
and not as shown in your post:
<item0>
a
</item0>
这将表示XML的内容有效内容的更改。
which would represent a change in the content payload of the XML.
这篇关于漂亮的打印XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!