2011-03-09 15 views
0

私はXStreamで作業していますが、特殊文字á、é、í、ó、úおよびñに問題があります。Xstream with special characters

私はこの試みた:私はXStream does no character encoding by itself, it relies on the configuration of the underlying XML writer. By default it uses its own PrettyPrintWriter which writes into the default encoding of the current locale. To write UTF-8 you have to provide a Writer with the appropriate encoding yourself.

私の問題は、私はライターを提供する方法がわからないということであることがわかっ

String charset = "UTF-8"; 
    xstream = new XStream(new DomDriver(charset)); 

(動作しない)

を...

// get the model from the map passed created by the controller 
Object model = map.get("model"); 

Object viewData = transformViewData(model); 

//TEST 
Writer w = new OutputStreamWriter(viewData, "UTF-8"); 
//FINTEST 

// if the model is null, we have an exception 
String xml = null; 
if (viewData != null){ 
    xml = xstream.toXML(viewData, w); //Err:Cannot find symbol... 
}else{ 
    // so render entire map 
    xml = xstream.toXML(map, w); //Err:Cannot find symbol... 
} 

response.getOutputStream().write(xml.getBytes()); 

答えて

1

最後に、動作しています!

私はそれが)(xml.getByteに "UTF-8" を追加する修正:

response.getOutputStream().write(xml.getBytes("UTF-8")); 
1

これはright there in the javadocです。

Writer w = new OutputStreamWriter(new FileOutputStream("test.xml"), "UTF-8"); 
XStream.toXML(object, w); 
+0

私はXMLファイルを持っていない、私は地図からデータを取得します。 – JMira

+0

新しいOutputStreamWriter(viewData、 "UTF-8")の行が無効です。 OutputStreamWriterは引数としてOutputStreamをとります。 XMLをどこに送りたいのですか? –

+0

Webブラウザには、その部分はうまく動作していますが、問題は特殊文字です。 – JMira