faqts : Computers : Programming : Languages : Python : Common Problems : XML

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 11 people (45%) answered Yes
Recently 2 of 8 people (25%) answered Yes

Entry

Is there a python script similar to shelve or pickle that will take an array and convert it to XML code?

Sep 2nd, 2000 02:34
unknown unknown, Michal Wallace


Problem:
We have several arrays, some are multidimensional arrays, created with
NumPy.  We'd like store these arrays in an XML data structure.  Is there
a python script similar to shelve or pickle that will take an array and
convert it to XML code?  Thanks.
Solution:
The xmlrpc module has routines to convert structures to XML (it's on
www.pythonware.com)
.. But if you want more control over what the XML looks like, you
could always start with something like:
def xmlify(somearray):
    res = "<cell>"
    for item in somearray:
        if type(item)==type([]):
            res = res + xmlify(item)
        else:
            res = res + item
    res = res + "</cell>"
    return res
print '<?xml version="1.0"?>'
print xmlify(yourMultiDimensionalArray)