外国文字(特にæøå)を含むxmlを解析しようとしていますが、解析に問題があります。私はエラーはありませんが、文字はこれと同じように解析されます。代わりに、私はちょうど文字に気づいた - 正しく表示されていません。 私は3つの文字のために.replaceAllを行うことができることを実現しますが、ここで問題が起こっているかどうかわからないのですが、どこかで間違いを犯しているか、replaceAllのルートを辿ることができません。DOMと特殊文字を使用したXML解析
コード:
private Document getDomElement(String xml) {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new ByteArrayInputStream(
xml.getBytes()));
// is.setCharacterStream(new StringReader(xml));
is.setEncoding("UTF-8");
Log.i(TAG, "Encoding: " + is.getEncoding());
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
// return DOM
return doc;
}
private String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
private final String getElementValue(Node elem) {
Node child;
if (elem != null) {
if (elem.hasChildNodes()) {
for (child = elem.getFirstChild(); child != null; child = child
.getNextSibling()) {
if (child.getNodeType() == Node.TEXT_NODE) {
return child.getNodeValue();
}
}
}
}
return "";
}
}
あなたはこれ以上のコードを参照する必要があるなら、私に教えてください。
ありがとうございました - ありがとうございます。
お寄せいただきありがとうございます。 私は以下を試しました: InputSource is = new InputeSource(new ByteArrayInpuStream(xml.getBytes( "UTF-8"))); InputSource = new InputSource(new StringReader(xml)); これらのアプローチのいずれも、何の違いもありませんでした。 is.setCharacterStreamはこの問題を解決しようとする試みの中で残っていますが、違いはありません。 – Line
@Line - おそらく、(getDomElement'の呼び出しの前に)xml文字列が最初に作成されたときにエンコーディングが乱れていた可能性があります。入力文字列に実際に期待する文字が含まれているかどうかを確認してください。 –
解析しようとしているxmlの例を以下に示します。 HttpResponse loginResponse = loginClient.execute(loginPost); HttpEntity responseEnt = loginResponse.getEntity(); 文字列result = EntityUtils.toString(responseEnt); //これはパーサーに与えられた文字列です。 EnitityUtils.toString()が問題を引き起こす可能性がありますか? 私は解析したxmlを使用して、ウェブサイトAPIから電話に解析された別の情報をチェックすることができます。 – Line