我的代碼不檢索包含特殊字符的整個元素節點。 例如,對於這個節點:用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