0
基於StackOverflow上的幾個示例,我有以下用於縮進XML的代碼。 我在String
中有一個源xml文件。然而,輸出不是縮進的,但它也不會給出任何錯誤。在調試器中檢查輸出,並且不包含空格或製表符等任何字符,這些字符可能會被錯誤地渲染並因此被忽略。如何在Android上正確縮進XML?
String input = "xmldata";
Source xmlInput = new StreamSource(new StringReader(input));
StringWriter stringWriter = new StringWriter();
StreamResult xmlOutput = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);
return stringWriter.toString();
我自己也嘗試設置indent-amount
到"2"
,但隨後的應用程序會抱怨一個未知屬性。可能這不是在Android中實現的。
難道我做錯了什麼嗎?是否有其他選項用於從源xml字符串生成縮進xml文件?
我想,一個也確實如此。它不適合我。我現在通過編寫自己的XML節點格式化程序來解決這個問題。 – Peterdk 2013-04-09 22:01:52