Android 4.0 冰淇淋三明治 Dom Parser 空指针
到目前为止我一直在使用这段代码来解析xml。它在以前版本的 android 中运行良好(直到 2.3.3)。
NodeList list = element.getElementsByTagName("course");
Element entry;
Element title;
if(list != null && list.getLength() > 0){
for(int i=0;i < list.getLength(); i++){
entry = (Element)list.item(i);
title = (Element)entry.getElementsByTagName("course").item(0);
biz_name_p = title.getAttribute("biz_name");
}
}
我有多个名称为“course”的标签,因此我将其放入列表中。在循环内,我检查元素课程并从中提取几个字符串值。
当我尝试 getAttribute("biz_name")
或任何其他属性时,它会抛出 NullPointerException
。
这在以前的版本中工作正常,直到 ICS...
当我用列表中的主标签替换“course”时(在整个 xml 中只重复一次), 我收到了字符串值,但只收到一次...他们是否改变了 Dom 解析器中的任何内容?
编辑
以下是我试图解析的xml..
<golf>
<course biz_name="Club Circle Golf Course"/>
<course biz_name="Club Circle Golf Course1"/>
<course biz_name="Club Circle Golf Course2"/>
</golf>
I have been using this code to parse xml till now. Its was working fine in previous version of android(till 2.3.3).
NodeList list = element.getElementsByTagName("course");
Element entry;
Element title;
if(list != null && list.getLength() > 0){
for(int i=0;i < list.getLength(); i++){
entry = (Element)list.item(i);
title = (Element)entry.getElementsByTagName("course").item(0);
biz_name_p = title.getAttribute("biz_name");
}
}
I have Multiple tags with name "course" so i am taking it in a List. Inside loop im checking for elements course and extracting several string values from it.
It throws NullPointerException
when i try to getAttribute("biz_name")
or any other attributes.
This was working fine in previous version until ICS...
When i Replace "course" with primary tag in the list(which only repeats once in whole xml),
i am receiving the string value but only once... Did they change any thing in Dom parser ??
EDIT
following is the xml im trying to parse..
<golf>
<course biz_name="Club Circle Golf Course"/>
<course biz_name="Club Circle Golf Course1"/>
<course biz_name="Club Circle Golf Course2"/>
</golf>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不明白为什么多次调用
getElementsByTagName
。那么你可以试试这个:I don't understand why
getElementsByTagName
is called multiple times. So can you try this: