another pattern: parsing xml element content that has elements in any order

Aleksander Slominski <[email protected]>
Newsgroups gmane.text.xml.xmlpull.devel
Message-ID <[email protected]>
"Duffey, Kevin" wrote:

> I have another question about the way xml is read/parsed. If the
> sequence of nodes is different from one xml file to the next, will the
> parsing be broke?

hi,

you need to be careful to write parsing code so it can handle such element
content and it is best described with simple pattern - i have put an initial
version of XML pull parsing patterns page, see this pattern described here:
http://www.extreme.indiana.edu/~aslom/xmlpull/patterns.html#any_order


> For the most part, the xml should be written in the
> same node/nesting order. But, for any reason (config file, for example),
> if the node order within a given level is not the same, will parsing
> break?

if you follow this pattern and use while loop together with if-else-if
statements to handle element content that is unordered then you are
safe and ultimately have more robust XML parsing code (that
corresponds to XML schemas xsd:all content model).

> Or is their a way to say "within this level, find me this node."
> So that even if you pass it up via the nextTag() or nextText() call, if
> you are still at the same level in the doc you can find ANY tag or get
> ANY text within that level?

that is not possible in full streaming parsing (unless you buffer events). it can be done
if you want to read XML content you can convert it into some kind of node tree
(like DOM, JDOM or even XmlPullNode) and then walk tree. this operation
is now easy with XMLPULL API  as we have SAX2 driver that can convert
XMLPULL events for any XML fragment into SAX2 callbacks..

however it is more efficient to use XMLPULL API directly and the pattern above.
by using it you avoid creation of temporary xml node tree in memory and then
need for walking it (using XMLPULL API directly allows for parsing XML in
fully streaming mode!)

thanks,

alek
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.