如何解决我的应用程序中的解析器错误?
我成功解析了我的 xml 文件,但现在我得到了 null 值。所以我不知道我在编码中犯了什么错误。我想在屏幕上显示我的字符串值。现在我试图以文本视图格式显示该字符串值,但我得到空值......
我的 xml 文件:
<Mobiles>
<Mobile>
<Phone>Nokia 1108</Phone>
<Network>GSM 900/1800 MHz</Network>
<Size>106x46x20 mm</Size>
<Ringtones>mono</Ringtones>
<SMS>yes</SMS>
<MMS>no</MMS>
<Email>no</Email>
<InstantMessaging >no</InstantMessaging>
</Mobile>
<Mobile>
<Phone>Nokia 1109</Phone>
<Network>GSM 900/1800 MHz</Network>
<Size>106x46x20 mm</Size>
<Ringtones>mono</Ringtones>
<SMS>yes</SMS>
<MMS>no</MMS>
<Email>no</Email>
<InstantMessaging >no</InstantMessaging>
</Mobile>
<Mobile>
<Phone>Nokia 1110</Phone>
<Network>GSM 900/1800 MHz</Network>
<Size>106x46x20 mm</Size>
<Ringtones>mono</Ringtones>
<SMS>yes</SMS>
<MMS>no</MMS>
<Email>no</Email>
<InstantMessaging >no</InstantMessaging>
</Mobile>
<Mobile>
<Phone>Nokia 1111</Phone>
<Network>GSM 900/1800 MHz</Network>
<Size>106x46x20 mm</Size>
<Ringtones>mono</Ringtones>
<SMS>yes</SMS>
<MMS>no</MMS>
<Email>no</Email>
<InstantMessaging >no</InstantMessaging>
</Mobile>
</Mobiles>
输出:
I parsed my xml file successfully, but now I am getting null value. so what mistake I make in my coding I don't know. I want to display my string value in my screen. Now I am trying to display that string value in text view format, but I am getting null value......
my xml file:
<Mobiles>
<Mobile>
<Phone>Nokia 1108</Phone>
<Network>GSM 900/1800 MHz</Network>
<Size>106x46x20 mm</Size>
<Ringtones>mono</Ringtones>
<SMS>yes</SMS>
<MMS>no</MMS>
<Email>no</Email>
<InstantMessaging >no</InstantMessaging>
</Mobile>
<Mobile>
<Phone>Nokia 1109</Phone>
<Network>GSM 900/1800 MHz</Network>
<Size>106x46x20 mm</Size>
<Ringtones>mono</Ringtones>
<SMS>yes</SMS>
<MMS>no</MMS>
<Email>no</Email>
<InstantMessaging >no</InstantMessaging>
</Mobile>
<Mobile>
<Phone>Nokia 1110</Phone>
<Network>GSM 900/1800 MHz</Network>
<Size>106x46x20 mm</Size>
<Ringtones>mono</Ringtones>
<SMS>yes</SMS>
<MMS>no</MMS>
<Email>no</Email>
<InstantMessaging >no</InstantMessaging>
</Mobile>
<Mobile>
<Phone>Nokia 1111</Phone>
<Network>GSM 900/1800 MHz</Network>
<Size>106x46x20 mm</Size>
<Ringtones>mono</Ringtones>
<SMS>yes</SMS>
<MMS>no</MMS>
<Email>no</Email>
<InstantMessaging >no</InstantMessaging>
</Mobile>
</Mobiles>
output:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需简单浏览一下您的代码,我会说问题在于您的
in_Mobiles
变量上的状态转换。从文档的开头到结尾,它始终为true
。因此,在
characters(char[], int, int)
方法中,第一个条件分支将消耗所有字符:在使用
in_Mobile
时会重复相同的行为,其中如果你解决了第一个问题,就会成为下一个罪魁祸首。编辑:
嗯,总的来说,你的解析器实现有点不稳定。尝试这样的事情:
首先,你的 ParsedExampleDataSet 有点不对劲。
将其转换为 Mobile 对象列表,如下所示:
接下来,创建一个名为 Mobile 的 bean 类,如下所示:
最后,需要重新设计您的 DefaultHandler 子类。像这样的东西应该有效。
并且,像这样运行它应该会产生结果:
Just briefly glancing at your code I'd say that the issue is the state transitions on your
in_Mobiles
variable. It will always betrue
from the start of the document to the end.In your
characters(char[], int, int)
method, the very first conditional branch will thus consume all characters:The same behavior repeats in the use of
in_Mobile
, which if you fix the first one, will be the next culprit.Edit:
Well, overall your parser implementation is kind of wonky. Try something like this instead:
First off, your ParsedExampleDataSet is a bit off.
Turn it into a List of Mobile objects instead, like this:
Next, make a bean class named Mobile, like this:
Finally, your DefaultHandler subclass needs to be reworked. Something like this ought to work.
And, just running it like this should produce a result:
为什么 endElement 方法中有 true ?
这可能会导致始终覆盖手机并且无法设置正确的字段。
why do you have true in endElement method?
this could leads to always override mobiles and not setting correct field.