2010-09-03 80 views
14

我很難嘗試使用XMLSerializer縮進XML文件。如何使用XMLSerializer正確縮進XML?

我已經試過

serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", 
         true); 

我試圖追加\nFileWriter,但輸出的是\n的和\t的在文件的開頭,而不是在正確的地方。我試過setPropery與代碼的正確URI等

部分:

XmlPullParserFactory parserFactory = XmlPullParserFactory.newInstance(); 
parserFactory .setNamespaceAware(true); 
XmlSerializer serializer = parserFactory .newSerializer(); 
File xmlFile = new File(PATH + ".xml");   
FileWriter writer = new FileWriter(xmlFile);    
serializer.setOutput(writer); 
//serializer.setProperty(INDENT_URL, INDENT); 
serializer.startDocument("UTF-8", null); 
//serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", 
         true); 
serializer.startTag(null, "bla"); 
writer.append('\n'); 

我缺少什麼?

回答

4

你試過在串行「聯合」使用這兩個屬性?

// indentation as 3 spaces 
serializer.setProperty(
    "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " "); 
// also set the line separator 
serializer.setProperty(
    "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n"); 
+8

是的。我做了,它給了我這個錯誤:java.lang.RuntimeException:Unsupported Property: at org.kxml2.io.KXmlSerializer.setProperty(KXmlSerializer.java:260).... – 2010-09-03 19:31:02

+2

這不起作用 – 2014-08-26 14:11:22

+0

@Eduardo Berton:這是不正確的答案,它不起作用 – 2016-06-09 06:22:21

3

這是Java中的一個解決方案,andriod支持變壓器,所以這應該工作。

// import additional packages 
import java.io.*; 

// import DOM related classes 
import org.w3c.dom.*; 
import javax.xml.parsers.*; 
import javax.xml.transform.*; 
import javax.xml.transform.dom.*; 
import javax.xml.transform.stream.*; 

// write the output file 
try { 
    // create a transformer 
    TransformerFactory transFactory = TransformerFactory.newInstance(); 
    Transformer  transformer = transFactory.newTransformer(); 

    // set some options on the transformer 
    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"); 

    // get a transformer and supporting classes 
    StringWriter writer = new StringWriter(); 
    StreamResult result = new StreamResult(writer); 
    DOMSource source = new DOMSource(xmlDoc); 

    // transform the xml document into a string 
    transformer.transform(source, result); 

    // open the output file 
    FileWriter outputWriter = new FileWriter(outputFile); 
    outputWriter.write(writer.toString()); 
    outputWriter.close(); 

} catch(javax.xml.transform.TransformerException e) { 
    // do something with this error 
}catch (java.io.IOException ex) { 
    // do something with this error 
} 
+0

:)今天我只是有很棒的google foo。 XML串行器將數據串行化爲XML,而文件寫入器則寫入文件。所以格式化爲人類可讀性的責任在於另一個類,如果它已經存在:) – JonWillis 2010-09-03 18:19:27

+2

我試過這個,但我一直得到非縮進文件:(儘管沒有錯誤 – Peterdk 2013-04-09 14:56:18

+1

如果我可以在這裏添加一些東西,一定要' ''replaceAll(「[\\ s] +」,「」)'''在字符串的情況下。在我的情況下,xml字符串已經有了'''\ n''',並且縮進沒有發生。 – 2015-04-21 08:55:33

35

serializer.setFeature(" http://xmlpull.org/v1/doc/features.html#indent-output ", true);現在工作。

我不知道我是否在serializer.startDocument(encoding, standalone)之前放置它,或者與創建.xml文件無關的錯誤!

謝謝你們!

+0

如何縮進評論呢? – accuya 2013-06-15 03:29:00

1

我只想做一個筆記,Transformer.setOutputProperties(Properties)似乎不適合我(1.6.0_26_b03),但Transformer.setOutputProperty(String,String)完美。
如果您有一個Properties對象,則可能需要迭代並單獨設置輸出屬性以使其工作。