我的代码不检索包含特殊字符的整个元素节点。 例如,对于这个节点:用Java读取包含特殊字符(&, - 等)的XML文档节点
<theaterName>P&G Greenbelt</theaterName>
它只会检索“P”由于符号。我需要检索整个字符串。
这里是我的代码:
public List<String> findTheaters() {
//Clear theaters application global
FilmhopperActivity.tData.clearTheaters();
ArrayList<String> theaters = new ArrayList<String>();
NodeList theaterNodes = doc.getElementsByTagName("theaterName");
for (int i = 0; i < theaterNodes.getLength(); i++) {
Node node = theaterNodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
//Found theater, add to return array
Element element = (Element) node;
NodeList children = element.getChildNodes();
String name = children.item(0).getNodeValue();
theaters.add(name);
//Logging
android.util.Log.i("MoviefoneFetcher", "Theater found: " + name);
//Add theater to application global
Theater t = new Theater(name);
FilmhopperActivity.tData.addTheater(t);
}
}
return theaters;
}
我尝试添加代码扩展名字符串来连接额外的children.items,但没有奏效。我只会得到“P &”。
...
String name = children.item(0).getNodeValue();
for (int j = 1; j < children.getLength() - 1; j++) {
name += children.item(j).getNodeValue();
}
谢谢你的时间。
UPDATE: 找到一个函数调用正常化(),您可以在节点调用,它结合了所有文本子节点这样一个children.item(0)包含了所有的儿童,包括与符号的文字!
简单地说,&XML不允许在XML内容中使用,因此您尝试解析的XML无效。 – dbrin 2012-02-04 04:08:25