Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Expat and nested elements

Reply
Thread Tools

Expat and nested elements

 
 
mathieu
Guest
Posts: n/a
 
      08-19-2008
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>
 
Reply With Quote
 
 
 
 
Bjoern Hoehrmann
Guest
Posts: n/a
 
      08-19-2008
* 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 · (E-Mail Removed) · http://bjoern.hoehrmann.de
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
build a hierarchical tree, without using DOM,schema, and sax using expat parser and c pharioum@gmail.com XML 2 11-05-2007 02:24 PM
Want help on how we convert output to tabular format Using the expat parser (http://expat.sourceforge.net/) i have to parse the following xml file and print it on the screen in tabular format. sharan XML 1 10-26-2007 01:20 PM
Using the expat parser (http://expat.sourceforge.net/) i have to parse the following xml file and print it on the screen in tabular format. Want a c program on that! sharan XML 1 10-26-2007 07:56 AM
libcurl and expat Sylvain/11XX XML 1 05-23-2006 08:54 PM
PyXML/expat and the empty elements Armin Steinhoff Python 1 05-17-2005 02:04 PM



Advertisments