Expat and nested elements

M

mathieu

Hi there,

I am struggling to write a piece of code that would parse a simple
xml file. I was wondering if there are good example (pattern?) to use
when dealing with nested xml elements.

For instance my xml looks like (*), with only one level of nesting.
AFAIK I need to do the book keeping myself esp. when reading the
CharacterData so that it is associated to the correct entry.

All I need to do is load the dict in some kind of matching C++
structure. This is not my homework, I am simply trying to use some
kind of pattern so that my code is readable later on.

Thanks
-Mathieu

(*)
<?xml version="1.0"?>
<dict>
<entry key="1" value="a">
<desc>blue</desc>
</entry>
<entry key="2" value="b">
<desc>red</desc>
</entry>
<entry key="3" value="c">
<desc>green</desc>
<entry key="2" value="d">
<desc>purple</desc>
</entry>
<entry key="4" value="e">
<desc>yellow</desc>
</entry>
<entry key="5" value="f">
<desc>orange</desc>
</entry>
</entry>
</dict>
 
B

Bjoern Hoehrmann

* mathieu wrote in comp.text.xml:
I am struggling to write a piece of code that would parse a simple
xml file. I was wondering if there are good example (pattern?) to use
when dealing with nested xml elements.

For instance my xml looks like (*), with only one level of nesting.
AFAIK I need to do the book keeping myself esp. when reading the
CharacterData so that it is associated to the correct entry.

All I need to do is load the dict in some kind of matching C++
structure. This is not my homework, I am simply trying to use some
kind of pattern so that my code is readable later on.

Well, a simple pattern is to use a stack, on start_element you push and
on end_element you pop; character data, comments, et al. are then always
associated with the element on top of the stack. Often you'll also have
an additional state variable to recall where you are and what you are
looking for, e.g. keeping track of preceding children of an element. On
the web are plenty of examples using expat, if those do not help, you'd
need to ask a more specific question.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top