[Zope] SAX problems

Chris Keyes chrisk@nipltd.com
Mon, 24 Sep 2001 21:28:33 +0100 (BST)


Hi all..

I'm trying to get a bit of Python code to work, and am getting the following 
error...

Python 2.1 (#1, Jul  3 2001, 17:35:22) 
[GCC 2.95.2 20000220 (Debian GNU/Linux)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> from StringIO import StringIO
>>> from xml.sax import make_parser
>>> from xml.sax.handler import feature_namespaces,ContentHandler
>>> class XML2DictHandler
(ContentHandler):                                             
...     def __init__(self):
...         self.dict={}
...     def startElement(self, name, attrs):
...         self.currentData = ''
...     def endElement(self, name):
...         self.dict[name]=self.currentData.encode('iso-8859-1')
...     def characters(self, someText):
...         self.currentData = self.currentData + someText
... 
>>> xml = """<transactiondata>\n         <months><![CDATA[1]]
></months>\n         <cartId><![CDATA[sas-Personal-1001355022]]
></cartId>\n         <transId><![CDATA[13022470]]></transId>\n         
<amount><![CDATA[35.25]]></amount>\n</transactiondata>"""
>>> file=StringIO(xml)
>>> theParser = make_parser()     
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/local/lib/python2.1/xml/sax/__init__.py", line 88, in make_parser
    raise SAXReaderNotAvailable("No parsers found", None)
xml.sax._exceptions.SAXReaderNotAvailable: No parsers found



When I try under debian I get:-
%>python2.1
Python 2.1 (#1, Jul  3 2001, 17:35:22) 
[GCC 2.95.2 20000220 (Debian GNU/Linux)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> xml = """<transactiondata>\n         <months><![CDATA[1]]
></months>\n         <cartId><![CDATA[sas-Personal-1001355022]]
></cartId>\n         <transId><![CDATA[13022470]]></transId>\n         
<amount><![CDATA[35.25]]></amount>\n</transactiondata>"""
>>> import xml2dict
>>> print xml2dict.xml2dict(xml)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "xml2dict.py", line 38, in xml2dict
    theParser = make_parser()    
  File "/usr/local/lib/python2.1/xml/sax/__init__.py", line 88, in make_parser
    raise SAXReaderNotAvailable("No parsers found", None)
xml.sax._exceptions.SAXReaderNotAvailable: No parsers found
>>> 



when I try on RedHat I get:-
$ python2.1
Python 2.1 (#1, Jun 22 2001, 14:05:22) 
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-81)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import xml2dict
>>> xml = """<transactiondata>\n         <months><![CDATA[1]]
></months>\n         <cartId><![CDATA[sas-Personal-1001355022]]
></cartId>\n         <transId><![CDATA[13022470]]></transId>\n         
<amount><![CDATA[35.25]]></amount>\n</transactiondata>"""
>>> print xml2dict.xml2dict(xml)
{u'cartId': 'sas-Personal-1001355022', u'amount': '35.25', 
u'transactiondata': '35.25\n', u'months': '1', u'transId': '13022470'}



I don't understand... I thought the xml.sax library was part of Python 2.1...?? 
Both installs would have come from source so anyone got any ideas how to fix??

If you do please cc me at chrisk@nipltd.com as I'm at home on webmail.

Cheers, and sorry if this is really a Python problem, but it is Zope related 
honest!!

ChrisK


The xml2dict code...
#############################################################
# XML handling
#
# - aDictionary = xml2dict(somexml)
#
#############################################################
from StringIO import StringIO
from xml.sax import make_parser
from xml.sax.handler import feature_namespaces,ContentHandler

# SAX Handler Class
class XML2DictHandler(ContentHandler):

    # --------------------------------------------------------------------------
    # Manage the data structures we build up as we process the file
    # --------------------------------------------------------------------------
    def __init__(self):
        self.dict={}
        
    # --------------------------------------------------------------------------
    # sax processing routines
    # --------------------------------------------------------------------------
    
    def startElement(self, name, attrs):
        self.currentData = ''

    def endElement(self, name):
        self.dict[name]=self.currentData.encode('iso-8859-1')
        
    def characters(self, someText):
        self.currentData = self.currentData + someText
        

def xml2dict(xml):
    # parse a lump of XML into a dictonary
    file=StringIO(xml)
    # Create a parser
    theParser = make_parser()    
    # Tell the parser we are not interested in XML namespaces
    theParser.setFeature(feature_namespaces, 0)    
    # Create the handler
    csh = XML2DictHandler()    
    # Tell the parser to use our handler
    theParser.setContentHandler(csh)            
    theParser.parse(file)
    return csh.dict