DocumentBuilder での文字化け

416 views
Skip to first unread message

yaguchi

unread,
Jul 5, 2011, 2:28:29 AM7/5/11
to 日本Androidの会
はじめまして、矢口と申します。

文字列をparseしてgetTextContent()にてテキストを取り出すと漢字が化けてしまいます。
簡単なサンプルで試してみましたがそれでも発生します。
ご教授いただけますでしょうか?

 String s = "<faultstring>漢字</faultstring>";
 @SuppressWarnings("deprecation")
 InputStream is = new StringBufferInputStream(s);
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
Document dom = factory.newDocumentBuilder().parse(is);
org.w3c.dom.Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("faultstring");
if (items.getLength() == 1){
   String tmp = items.item(0).getTextContent();
//tmpの値が"漢字"でなく wとなってしまう。
}

大垣憲俊

unread,
Jul 5, 2011, 7:37:27 AM7/5/11
to android-g...@googlegroups.com
大垣です。

StringBufferInputStreamのjavadocには、以下の記述があります。

「推奨されていません。 このクラスでは、文字からバイトへの変換が正しく行われません。JDK 1.1 以降は、文字列からストリームを作成するには、StringReader クラスを使用するのが適切です。」

そこで、以下のようにしてみたところ、文字化けしませんでした。

//InputStream is = new StringBufferInputStream(s);

  InputSource is = new InputSource(new StringReader(s));



yaguchi

unread,
Jul 6, 2011, 2:23:28 AM7/6/11
to 日本Androidの会

ばっちり直りました。
javaに不慣れで適切なクラスが使えてないです。

ありがとうございます。
Reply all
Reply to author
Forward
0 new messages