如何使 XmlPullParser 返回顶级元素的列表?
我一直在使用 XmlPullParser 从本地存储的 xml 文档在 Android 应用程序上生成表单。
XmlPullParser xpp = getResources().getXml(R.xml.calculator);
int eventType = xpp.getEventType();
然后我有一个 while 循环来处理树中的所有元素。然而,因为我一直使用
eventType = xpp.next();
这是一种深度优先的方法,而实际上我想要一种广度优先的方法来做事(用户通过提问来向下钻取树)。大多数 xml 教程都依赖 DOM,我听说由于内存限制,DOM 在 Android 上不可靠。我将非常感谢您的帮助。
xml 采用这种形式,
<top>
<page>
<question>This is the first question</question>
<answer>
<text>Answer 1</text>
<page>
<question>If you choose Answer 1 you get asked this question</question>
<answer>
.
.
</answer>
<answer>
.
.
</answer>
</page>
</answer>
<answer>
<text>Answer 2</text>
.
.
.
</answer>
</page>
</top>
我希望这比以前更有意义。因此,实际上,给定这棵树,我想生成屏幕(在运行时)询问用户这些问题并接受他们的答案。根据他们的答案,他们会得到下一组问题。感谢您的快速回复!
I've been using XmlPullParser to generate forms on an android application from a locally stored xml document.
XmlPullParser xpp = getResources().getXml(R.xml.calculator);
int eventType = xpp.getEventType();
and then I have the while loop that processes all the elements in the tree. However, because I keep using
eventType = xpp.next();
this is a Depth-first method when in fact I want a Breadth-first method of doing things (the user drills down the tree by asking questions). Most of the xml tutorials rely on DOM which I've heard is unreliable on android because of memory constraints. I would be very grateful for help.
The xml is in this form
<top>
<page>
<question>This is the first question</question>
<answer>
<text>Answer 1</text>
<page>
<question>If you choose Answer 1 you get asked this question</question>
<answer>
.
.
</answer>
<answer>
.
.
</answer>
</page>
</answer>
<answer>
<text>Answer 2</text>
.
.
.
</answer>
</page>
</top>
I hope that makes a little more sense than it did before. So in effect, given this tree, I want to generate screens (at run-time) that ask the user these questions and take their answers in. Depending on their answers, they get the next set of questions. Thanks for the really quick responses!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只想获取顶级元素(文档根元素内的元素),那么我建议保留当前元素的
Stack
。当您点击开始时,将元素名称压入堆栈。当您停止时,将元素名称从堆栈中弹出。每当堆栈上有一个元素时,您就有一个顶级元素。If you want to just get the top level elements (the ones inside the documents root element), then I would suggest keeping a
Stack
of the current element(s). When you hit a start, push the element name onto the stack. When you hit a stop, pop the element name off the stack. Whenever you have a single element on the stack, you have yourself a top level element.在我看来,面包优先算法直观上会更有效地获取第一级节点。然而,这似乎是一种非常“人性化”的做法。我想不出 xml 解析器在读取开始标记时可以猜测第一级标记在哪里结束的方法。
因此,无论如何,您的解析器将必须读取整个文件,堆叠所有节点。也许可以避免堆叠,但与实际对 xml 结构本身进行评级相比,它似乎微不足道。所以收益不会很大。
您是否考虑过使用一个数据库和两个具有一对多关系的表?
它既可以解决效率问题,也可以解决应用程序能够处理的数据量问题。
问候,
史蒂芬
It seems to me that breadfirst algorithm would be intuitively more efficient to get the first level nodes. However, this seems to be a very "human" approach. I can't think of a way an xml parser could guess where a first level tag ends when it reads the start tag.
So, anyhow, your parser will have to read the whole file, stacking all nodes. Maybe stacking could be avoided but it seems like a peanut compared to actually rading the xml structure itself. So gains wouldn't be huge.
Did you consider using a database and two tables with a one to many relation ?
It would both solve efficiency and the amount of data your app would be able to deal with.
Regards,
Stéphane
如果您不想使用 DOM,那么您将需要从深度优先的角度从 XML 构建您自己的文档对象模型。构建模型后,您可以应用广度优先语义。
如果您担心内存,那么没有任何内容表明您的对象模型必须位于内存中...您可以在解析它时将其存储在 SQLLite 数据库中。这可以帮助您避免多次解析 XML 并更好地与您的应用程序集成。
没有其他细节,我只能告诉你这些。
If you don't want to use DOM then you're going to need to build up your own object model of the document from XML from a depth first perspective. After you've built up the model you can then apply breadth-first semantics.
If you're concerned about memory, there's nothing that says your object model has to be in memory... you could store it in a SQLLite database as you're parsing it. That may help you avoid parsing the XML multiple times and integrate in better with your app.
Without any other details, that's about all I can tell you.