473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Expat and nested elements

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>
Aug 19 '08 #1
2 1508
* 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:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Aug 19 '08 #2
On Aug 19, 11:29*am, Bjoern Hoehrmann <bjo...@hoehrmann.dewrote:
* 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.
The only examples I found using expat are extremely simple. Do you
have a link in mind, or simply the google query you tried.

Thanks!
Aug 19 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Will Stuyvesant | last post by:
There seems to be no XML parser that can do validation in the Python Standard Libraries. And I am stuck with Python 2.1.1. until my web master upgrades (I use Python for CGI). I know pyXML has...
1
by: Armin Steinhoff | last post by:
Hi all, is it possible to get the name of an empty element specified by <element/> ... the expat parser seems only to recognize the <element></element> form Regards Armin
1
by: David | last post by:
Hi, I'm trying to parse an xml file and am a bit confused. I have created my class XmlParser. Also I have 3 other questions. 1-How to the GetAttribute to search for the value of a specific...
1
by: Sridhar | last post by:
HI, I am beginner to Expat and currently started learning. What I understood about Expat is Expat is freeware It is good XML Parser Provides C-API
5
by: horndude77 | last post by:
Ok, this might be for a web designing group, but here's my problem. I'm trying to make a web page with tabs which you can navigate between without the page reloading. I have one set of tabs working...
4
by: Maarten Verhage | last post by:
Hi All, I've made a modified version of the Expat elements.c example. What I want the program to do is to recognize a hardcoded element-name/tag-name and then execute some code which has access...
2
by: dwelch91 | last post by:
Hi, c.l.p.'ers- I am having a problem with the import of xml.parsers.expat that has gotten me completely stumped. I have two programs, one a PyQt program and one a command line (text) program...
1
by: =?Utf-8?B?SmVyZW15X0I=?= | last post by:
I am working on an order entry program and have a question related to deserializing nodes with nested elements. The purchase order contains multiple line items which I select using an...
1
by: vadlapatlahari | last post by:
Hi, I get the following error with Expat while configuring my application server. Can anyone suggest a solution? When i do an ldd, i get the following : $ldd Expat.so Expat.so needs:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.