[Tutor] make_parser

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Sep 7 20:53:42 CEST 2004



On Tue, 7 Sep 2004, Ajay wrote:

> in my code, i am giving make_parser the name of the parser i'd like to
> use the code is below
>
> parser = make_parser(['xmlproc'])


Hi Ajay,

Next time, also show us what imports you've done.  There's no indication
that you're using xml.sax.make_parser(), so folks who haven't played with
the XML stuff would have a harder time to get your example to work.


> this throws an error saying ExpatParser instance has no attribute
> 'parseString'

Ok, it sounds like the 'parser' instance that you're getting back isn't an
xmlproc instance.  It's very possible that the system isn't finding
xmlproc.  For example, if we pass something wacky to
xml.sax.make_parser, it'll just give us an expat parser by default:

###
>>> import xml
>>> import xml.sax
>>> xml.sax.make_parser(['foobar!!'])
<xml.sax.expatreader.ExpatParser instance at 0x402fc3cc>
###

Since xmlproc is not a part of the Standard Library, so I'd recommend
check this first.  Have you installed it from pyxml.sourceforge.net?


I don't know why in the world there isn't good web-accessible
documentation for PyXML.  I was able to construct an XMLProc parser
instance this way:

###
>>> xml.sax.make_parser(['xml.sax.drivers2.drv_xmlproc'])
<xml.sax.drivers2.drv_xmlproc.XmlprocDriver instance at 0x4038d80c>
###

but this does not feel obvious at all to me.  *sigh*



Anyway, the error you're getting is actually an expected one:
parseString() is a convenience function of the xml.sax package, but it's
not a member of the XMLReader class.

    http://www.python.org/doc/lib/xmlreader-objects.html

describes what methods you can pass to it.  Use parser.parse().  For
example:



###
>>> p = xml.sax.make_parser(['xml.sax.drivers2.drv_xmlproc'])
>>> from StringIO import StringIO
>>> p.parse(StringIO("<hello>world</hello>"))
>>> p.reset()
>>>
>>>
>>> p.parse(StringIO("<hello>world</hi>"))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.3/site-packages/_xmlplus/sax/xmlreader.py", line
125, in parse
    self.close()
  File
"/usr/lib/python2.3/site-packages/_xmlplus/sax/drivers2/drv_xmlproc.py",
line 99, in close
    self._parser.flush()
  File
"/usr/lib/python2.3/site-packages/_xmlplus/parsers/xmlproc/xmlutils.py",
line 361, in flush
    self.do_parse()
  File
"/usr/lib/python2.3/site-packages/_xmlplus/parsers/xmlproc/xmlproc.py",
line 91, in do_parse
    self.parse_end_tag()
  File
"/usr/lib/python2.3/site-packages/_xmlplus/parsers/xmlproc/xmlproc.py",
line 344, in parse_end_tag
    self.report_error(3023,(name,elem))
  File
"/usr/lib/python2.3/site-packages/_xmlplus/parsers/xmlproc/xmlproc.py",
line 63, in report_error
    EntityParser.report_error(self,number,args)
  File
"/usr/lib/python2.3/site-packages/_xmlplus/parsers/xmlproc/xmlutils.py",
line 524, in report_error
    self.err.fatal(msg)
  File
"/usr/lib/python2.3/site-packages/_xmlplus/sax/drivers2/drv_xmlproc.py",
line 229, in fatal
    self._err_handler.fatalError(saxlib.SAXParseException(msg, None,
self))
  File "/usr/lib/python2.3/site-packages/_xmlplus/sax/handler.py", line
38, in fatalError
    raise exception
xml.sax._exceptions.SAXParseException: <unknown>:1:17: End tag for 'hi'
seen, but 'hello' expected
###


Hope this helps!



More information about the Tutor mailing list