Questions about parsing XML

K

Kyle Root

I'm writing a practice program that parses an xml config file, at the
moment I'm using expat. However I don't like some of it's behaviors.
For example ParseFile() will parse the *entire* file and I don't want to
do that. I would like to be able to tell it to find such and such a tag
and return the cdata it contains, is this at all possible? If it is how
would I handle optional tags?

If it's not possible and the entire file does have to be parsed in one
fell swoop, are there any special ways collecting the data into a usable
form? I tried to put everything into a list, but that's not very
practicle, especially with optional tags.

Thanks,
Kyle
 
J

Jarek Zgoda

Kyle Root said:
I'm writing a practice program that parses an xml config file, at the
moment I'm using expat. However I don't like some of it's behaviors.
For example ParseFile() will parse the *entire* file and I don't want to
do that. I would like to be able to tell it to find such and such a tag
and return the cdata it contains, is this at all possible? If it is how
would I handle optional tags?

libxml2 (http://www.xmlsoft.org/) has excellent support for XPath, F.
Lundh's ElementTree also supports some XPath expressions, although it is
built on expat and needs to parse whole XML document.
 
K

Kyle Root

Jarek said:
libxml2 (http://www.xmlsoft.org/) has excellent support for XPath, F.
Lundh's ElementTree also supports some XPath expressions, although it is
built on expat and needs to parse whole XML document.

Well I was going to use that, but I remember reading somewhere that
xmlib was depreciated or something, even if it has been do you still
recommend libxml2 over expat?
 
J

Jarek Zgoda

Kyle Root said:
Well I was going to use that, but I remember reading somewhere that
xmlib was depreciated or something, even if it has been do you still
recommend libxml2 over expat?

Python's xmllib (regular expression based parser) is not the same
software as libxml2 (GNOME XML library) ;).

If you need full support for XPath, XPointer, XInclude or namespaces
(see http://www.xml.com/pub/a/2004/06/30/py-xml.html) I recommend using
libxml2. Some people say that some of its parsers are also faster than
original J. Clark's expat (although not as easy to use as expat or
expat-like SAX parser from libxml2).
 
K

Kyle Root

Jarek said:
Python's xmllib (regular expression based parser) is not the same
software as libxml2 (GNOME XML library) ;).

If you need full support for XPath, XPointer, XInclude or namespaces
(see http://www.xml.com/pub/a/2004/06/30/py-xml.html) I recommend using
libxml2. Some people say that some of its parsers are also faster than
original J. Clark's expat (although not as easy to use as expat or
expat-like SAX parser from libxml2).

Okay thank you very much!
I like libxml2's docs better than the Python Library Reference anyways. :D
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top