漂亮的打印XML文件

时间:2015-11-17 20:53:41

标签: java xml xslt dom java-8

原始问题

我正在尝试在没有任何外部库的情况下绘制XML文件,但是无法让Java做我想要的...这是我的代码到目前为止(我添加了类似问题的任何解决方案!):

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>

我收到的输出如下:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>

<test>
<item0>a</item0>
<item1>b</item1>
    </test>
</root>

现在我没有得到的是为什么在<root>之后有一个空行和</test>之前的缩进,但是没有其他地方。在这个新文件上运行代码不会改变任何内容!

我希望我的输出文件看起来像这样:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
    <test>
        <item0>a</item0>
        <item1>b</item1>
     </test>
 </root>

更新

我从我的代码中删除了一些显然没有做任何事情的行:

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

现在,这会从原始问题的单行文件中创建一个漂亮的打印文件,以便回答部分问题!我不知道究竟是什么问题,但无论如何,现在有效^^

但我也有一些旧的文件可供我的程序读取和写入,看起来像这样:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<test>
<item0>a</item0>
<item1>b</item1>
</test>
</root>

每个节点后都有一个换行符,但没有缩进。我的代码保持文件不变...如何更正?

2 个答案:

答案 0 :(得分:2)

您已标记此XSLT,并且如果应用以下XSLT样式表:

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输入,结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <test>
        <item0>a</item0>
        <item1>b</item1>
    </test>
</root>

现场演示:http://xsltransform.net/ncdD7mg

请注意,这些项目“非常印刷”为:

<item0>a</item0>

而不是您帖子中显示的内容:

<item0>
    a
</item0>

表示XML内容有效负载的更改。

答案 1 :(得分:0)

当您简单地使用the identity rule 进行转换时,为什么要烦恼复杂且容易出错的Java代码,如下所示:

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output indent="yes"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

在提供的XML文档上应用此转换时

<?xml version="1.0" encoding="UTF-8" standalone="no"?><root><test><item0>a</item0><item1>b</item1></test></root>

产生了一个漂亮的,更重要的是统一的(每个XSLT处理器)输出

<?xml version="1.0" encoding="utf-8"?>
<root>
   <test>
      <item0>a</item0>
      <item1>b</item1>
   </test>
</root>