解析XML和后删除所有字符放大器;放大器; [英] Parsing XML dropping all characters after &
问题描述
我创建一些解析XML和在ListView显示它的应用程序。在我的XML的几个项目包括&安培;
如此我已经逃脱他们这样&放大器;放大器;
这是工作正确的一些设备和仿真器。
I am creating an app that parses some XML and display it in a ListView. A few items in my xml contain &
s so I have escaped them like this &
It is working correctly on a few devices and on the emulator.
但在两个设备(三星Sidekick的4克API 2.2,而三星Replish API 2.3.6),它是失败的。一切&放大器后,放大器;
被神奇地消失了。
But on two devices (Samsung Sidekick 4g API 2.2, and Samsung Replish API 2.3.6) it is failing. Everything after the &
gets magically disappeared.
下面是在XML的项目给我的麻烦:
Here is the item in the XML giving me trouble:
<site>
<name>English Language & Usage</name>
<link>http://english.stackexchange.com/</link>
<about>English Language & Usage Stack Exchange is a question and answer site for linguists, etymologists, and serious English language enthusiasts.</about>
<image>https://dl.dropboxusercontent.com/u/5724095/XmlParseExample/english.png</image>
</site>
下面是解析code的肉:
Here is the "meat" of the parsing code:
private static String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
Log.i("StackSites", ""+getElementValue(n.item(0)));
return getElementValue(n.item(0));
}
private static 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 "";
}
在某些设备(LG擎天柱G,摩托Attrix 2,和几个模拟器)这个工作正常并且出来是这样的:
On some devices (LG Optimus G, Moto Attrix 2, and a few emulators) this works correctly and comes out like this:
不过上,我试过两个三星设备的getValue()
方法只返回&功放之前出现的文本;放大器;
这样的结果是这样的:
However on the two Samsung devices that I've tried getValue()
method returns only the text that comes before the &
so the result is this:
推荐答案
这是因为你不看的其余节点。实体获得一个不同的节点,之后该实体之后的文本获取的节点。您立即返回 - 你需要连接你的结果。
That's because you aren't looking at the rest of the nodes. The entity gets a different node, and the text following the entity gets the node after that. You are returning immediately -- you need to concatenate your results.
这篇关于解析XML和后删除所有字符放大器;放大器;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!