开发者

How do you make XmlPullParser return lists of top level elements?

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>
         <tex开发者_运维知识库t>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!!


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜