[Zope] Flash XMLRPC clients for Zope ....

Steve Spicklemire steve@spvi.com
Mon, 6 Aug 2001 03:51:42 -0500


Oops... sorry.. somehow handleXML got nipped.. this might be handy for 
someone else wanting simple xml converted to dicts for easy python 
processing..

On Monday, August 6, 2001, at 03:43 AM, Steve Spicklemire wrote:

>
> Hi Nigel,
>
> On Monday, August 6, 2001, at 12:18 AM, Nigel Head wrote:
>
>> I saw an enquiry a while back about Flash XML-RPC clients. After some
>> spelunking I can add my 2 eurocents to the situation ...  
>> unfortunately not
>> good news.


handleXML:

#
# Handle XML input from Flash.
# Needs ParsedXML product installed.
#

if __name__=="__main__":
     """ For in 'IDE' testing.. """
     import sys
     sys.path.append('/usr/local/etc/Zope2b/lib/python')
     import ZODB

from Products.ParsedXML.DOM import ExpatBuilder

try:
     from cStringIO import StringIO
except ImportError:
     from StringIO import StringIO

def handleXML(xmlString, debug=0):
     return toDict(DOMParseString( None, xmlString), debug=debug)

def DOMParseString(self, xml):
     file = StringIO(xml)
     return ExpatBuilder.parse(file)

def toDict( xmlThing, currDict=None, debug=0 ):

     if currDict is None:
         currDict = {}

     if hasattr(xmlThing,'data'):
         currDict['data'] = xmlThing.data

     if hasattr(xmlThing, '_attributes'):
         if debug:
             print "checking attributes:",  xmlThing._attributes

         for attr in xmlThing._attributes:
             if currDict.has_key(attr[1]):
                 oldVal = currDict[attr[1]]
                 if type(oldVal)==type(''):
                     currDict[attr[1]]=[oldVal, attr[4]]
                 else:
                     oldVal.append(attr[4])
             else:
                 currDict[attr[1]] = attr[4]

     if hasattr(xmlThing, '_children'):
         for subThing in xmlThing._children:
             newDict = toDict(subThing)

             if hasattr(subThing, 'data'):
                 currDict['data'] = subThing.data

             elif hasattr(subThing, 'nodeName'):
                 if currDict.has_key(subThing.nodeName):
                     oldVal=currDict[subThing.nodeName]
                     if type(oldVal)==type({}):
                         currDict[subThing.nodeName]=[oldVal, newDict]
                     else:
                         oldVal.append(newDict)
                 else:
                     currDict[subThing.nodeName] = newDict

     return currDict

theXML = """<?xml version = "1.0"?>
<CD title="2 Against Nature">
<track title = "gaslighting abbie"/>
<track title = "what a shame about me"/>
<track title = "two against nature"/>
<track title = "janie runaway"/>
<track title = "almost gothic"/>
<track title = "jack of speed"/>
<track title = "cousin dupree"/>
<track title = "negative girl"/>
<track title = "west of hollywood"/>
<relatedItems artist = "Steely Dan"/>
<relatedItems artist = "Walter Becker"/>
<relatedItems artist = "Donald Fagen"/>
</CD>
"""

if __name__=="__main__":

     #print handleXML('<querySources level="/my/favorite/place"/>')
     print handleXML(theXML, debug=1)