2010-08-26 65 views
5

我試圖保存一棵樹(擴展JTree),其中包含一個XML文檔到一個DOM Object改變了它的結構。Java,XML DocumentBuilder - 解析時設置編碼

我創建了一個新的文檔對象,走過樹成功地檢索內容(包括XML文檔的原始編碼),現在有一個ByteArrayInputStream其中有樹的內容(XML文件)與正確的編碼。

問題是當我解析ByteArrayInputStream時,編碼自動變爲UTF-8(在XML文檔中)。

有沒有辦法來防止這種情況,並使用ByteArrayInputStream中提供的正確編碼。

值得一提的是,我已經使用了
transformer.setOutputProperty(OutputKeys.ENCODING, encoding)方法來檢索正確的編碼。

任何幫助,將不勝感激。

+0

你可以分享一下你的代碼嗎? – gawi 2010-08-26 18:59:23

回答

2

我解決了它,給了很多試驗和錯誤。

我用

OutputFormat format = new OutputFormat(document); 

但它改成

OutputFormat format = new OutputFormat(d, encoding, true); 

,這解決了我的問題。

encoding是我設定它爲
true是指是否設置了縮進。

請注意自我閱讀更仔細 - 我曾在幾個小時前看過javadoc - 如果只是我會仔細閱讀。

3
// Read XML 
String xml = "xml" 
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder = factory.newDocumentBuilder(); 
Document document = builder.parse(new InputSource(new StringReader(xml))); 

// Append formatting 
OutputFormat format = new OutputFormat(document); 

if (document.getXmlEncoding() != null) { 
    format.setEncoding(document.getXmlEncoding()); 
} 

format.setLineWidth(100); 
format.setIndenting(true); 
format.setIndent(5); 
Writer out = new StringWriter(); 
XMLSerializer serializer = new XMLSerializer(out, format); 
serializer.serialize(document); 
String result = out.toString(); 
+11

此代碼的某些解釋對於稍後來閱讀此答案的人員會很有用。 – 2012-12-17 14:59:17

3

這裏是因爲OUTPUTFORMAT更新的答案被棄用:

TransformerFactory tf = TransformerFactory.newInstance(); 
Transformer transformer = tf.newTransformer(); 
transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); 

StringWriter writer = new StringWriter(); 
transformer.transform(new DOMSource(document), new StreamResult(writer)); 
String output = writer.getBuffer().toString().replaceAll("\n|\r", ""); 

第二部分將返回XML文檔作爲字符串

0

這個工作對我來說,是非常簡單的。不需要變壓器或輸出格式器:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder = factory.newDocumentBuilder(); 
InputSource is = new InputSource(inputStream); 
is.setEncoding("ISO-8859-1"); // set your encoding here 
Document document = builder.parse(is);