[英]pretty print XML file
我試圖在沒有任何外部庫的情況下對一個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>
它們在每個節點后都有換行符,但沒有縮進。 我的代碼保持文件不變...我該如何糾正?
您已標記此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內容有效負載的變化。
當您可以使用標識規則進行簡單轉換時,為什么還要使用復雜且容易出錯的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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.