【发布时间】:2012-09-21 07:31:51
【问题描述】:
我试图获取带有一些 Unicode 字符的 XML 输出。我无法读取标签内的完整字符串,只能读取一个。
这是我的 XML 输出
<item>
<id>1</id>
<name>ලොල්</name>
<cost>155</cost>
<description>ලො</description>
</item>
这是我用来解析 XML 字符串的 java 代码。
public Document getDomElement(String xml) {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setEncoding("UTF-16");
is.setCharacterStream(new StringReader(xml));
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;
}
当我使用普通英文字符时,它会给出完整的字符串。
【问题讨论】:
-
当您尝试解析非英语字符时会发生什么?字符串不正确?还是失败了?
-
它不会失败。它只读取第一个字符。在这个例子中它只输出 ල不是 ලොල්
-
哦,好的。但接下来有两件事:
valueOfTheContainedText.length()返回 1 还是 4?以及 xml,如果你在解析之前打印它,是吗?
标签: java android xml-parsing xml-serialization