0
<?xml version="1.0" encoding="UTF-8"?>
我會放一些代碼提取,我認爲這是有意義的。從XML到Java的字符編碼
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
如果我打印字符串XML到屏幕上,我可以看到的編纂工作已經
一些問題:我從一個XML通過HTTP請求,這樣的閱讀一些資料
然後返回一個文件我有這個
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
雖然我正確地獲取從h的信息ttp請求,我在顯示數據時遇到了角色的使用問題。
我已經試過is.setEncoding(「UTF-8」)但沒有奏效。
感謝您的回覆,是不是可能的問題是與XML而不是代碼? 而不是做EntityUtils.toString,我可以使用你的建議,但那麼我應該把這個對象的構造函數? is.setCharacterStream(new StringReader(xml)); //之前,當它是一個字符串 is.setCharacterStream(???); //我不知道哪個構造函數可以用於字節數組。 – unpix 2013-03-18 14:34:16
@unpix使用[InputSource(InputStream)'構造函數](http://docs.oracle.com/javase/7/docs/api/org/xml/sax/InputSource.html#InputSource%28java.io.InputStream %29)。如果您以字節數組開頭,則可以使用[ByteArrayInputStream](http://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html#ByteArrayInputStream%28byte []%29)。 – McDowell 2013-03-18 15:04:06
謝謝,也許它會工作,但我已經找到了最簡單的解決方案 – unpix 2013-03-18 15:07:19