[XML-SIG] Error using xml.sax.{xmlreader,expatreader}

Martin v. Loewis martin@loewis.home.cs.tu-berlin.de
Wed, 29 Nov 2000 18:01:42 +0100


> www.python.org, "Documentation", "Topic Guides", XML, XML HOWTO.  I printed
> it November 18.  But it looks like it doesn't matter now; I looked today
> and the pages have been reorganized.

That's hard to believe - if you look at the "Last-Modified" field of
the XML howto, you see it had been updated November 3 or earlier. But
as you say - that doesn't matter now.

> 
> BTW, today http://www.python.org/topics, "XML" link goes to 
> http://pyxml.sourceforge.net/topics/ (the same page) via a redirect,
> then clicking the "XML" link again produces 
> "The requested URL /topics/xml/ was not found on this server."

On http://pyxml.sourceforge.net/topics/, I cannot see a "XML" link,
other than the one pointing to
http://dmoz.org/Computers/Data_Formats/Markup_Languages/XML/.

> OK, I didn't understand the entire SAX section in the Library
> manual; that's why I was following the example in the HOWTO.  The
> manual explains what each function/class/method/constant is, but it
> doesn't say which few are the most inportant for somebody wanting to
> parse a simple document.  Adding an example would make a world of
> difference.

Indeed, that is a reference manual; not every module's description has
an example.

> How about the little program I posted?  Could it not be the basis of
> an example if it were shorter, had the errors removed, used
> xml.sax.parser(), and printed the keys/values as it puts them into
> the dictionary?  I can write something up if somebody wants to be an
> "expert reviewer".

That would be good. You can find the raw source of the documentation at
http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/python/dist/src/Doc/lib/xmlsax.tex?cvsroot=python

Please submit patches to sourceforge.net/projects/python.

> Regarding XMLGenerator, again, an example would make a world of
> difference.  Reading the following paragraph, I did not get any
> indication that this is the class I should use to convert my data
> structure to XML.  "This class implements the ContentHandler
> interface by writing SAX events back into an XML document."  Great,
> but I have a list of dictionaries, not SAX events, and I don't care
> about SAX per se, I just want to get the data written somehow.

I don't know what you are trying to achieve with those dictionaries,
but it sounds as if SAX is not for you, then. Nor would I think the
DOM is any help.

In general, the difficult part of XML processing is to parse it, not
to generate it. A few print statements normally will generate perfect
XML.

> "In other words, using an XMLGenerator as the content handler will
> reproduce the original document being parsed."  What!?  Why would
> anybody want to "reproduce" the XML document they had just read in?
> They already have it.

Your analysis is correct - nobody would use this class as it is,
unless she has written a new SAX parser and wants to test whether it
operates correctly.

> Looking at the source, I can see I can use it as a standalone class
> to write my XML file, but I would have never guessed that from the
> Library reference, much less known how to use it.

I don't understand that remark. How exactly would you use it? You
would instantiate it, and then call its methods???? I hope you
wouldn't, and I certainly did not think of this use when I wrote the
documentation.

Say you have a name/age dictionary which you want to write back into
some XML DTD. What's wrong with

print '<?xml version="1.0"?>'
print '<people>'
for item in dict.items():
  print "<person><name>%s</name><age>%d</age></person>" % (name, age)
print '</people>'

sounds to much much more natural than creating an XMLGenerator...

> Shall I write up an example for that too?

If it is *that* use, then I don't think an example would be good.

Regards,
Martin