Expat and nested elements

Discussion in 'XML' started by mathieu, Aug 19, 2008.

  1. mathieu

    mathieu Guest

    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>
     
    mathieu, Aug 19, 2008
    #1
    1. Advertising

  2. * 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.
    --
    Björn Höhrmann · mailto: · http://bjoern.hoehrmann.de
     
    Bjoern Hoehrmann, Aug 19, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sylvain/11XX

    libcurl and expat

    Sylvain/11XX, May 23, 2006, in forum: XML
    Replies:
    1
    Views:
    581
    Joe Kesselman
    May 23, 2006
  2. Operation Latte Thunder

    freeze and expat

    Operation Latte Thunder, Apr 21, 2005, in forum: Python
    Replies:
    0
    Views:
    363
    Operation Latte Thunder
    Apr 21, 2005
  3. Armin Steinhoff

    PyXML/expat and the empty elements

    Armin Steinhoff, May 17, 2005, in forum: Python
    Replies:
    1
    Views:
    284
    Armin Steinhoff
    May 17, 2005
  4. sharan
    Replies:
    1
    Views:
    731
    Pavel Lepin
    Oct 26, 2007
  5. Replies:
    2
    Views:
    956
    Joseph Kesselman
    Nov 5, 2007
Loading...

Share This Page