Some of the information here may be outdated, please check the book instead
[edit]

Now that it is needed since web2py exports already in CSV but you can do define

def export_xml(rows):
    idx=range(len(rows.colnames))
    colnames=[item.replace('.','_') for item in rows.colnames]
    records=[]
    for row in rows.response: records.append(TAG['record'](*[TAG[colnames[i]](row[i]) for i in idx]))
    return str(TAG['records'](*records))

Now if you have a model like

db=SQLDB('sqlite://test.db')
db.define_table('mytable',SQLField('myfield'))
for i in range(100): db.mytable.insert(myfield=i)

you can get your data in XML by doing

print export_xml(db().select(db.mytable.ALL))

Notice that

TAG.name('a','b',c='d')

and

TAG['name']('a','b',c='d')

both generate the following XML

<name c="d">ab</name>

where 'a' and 'b' and 'd' are escaped as appropriate. Using TAG you can can generate any HTML/XML tag you need and is not already provided in the API. TAGs can be nested and are serialized with str()

© 2008-2010 by Massimo Di Pierro - All rights reserved - Powered by web2py - design derived from a theme by the earlybird
The content of this book is released under the Artistic License 2.0 - Modified content cannot be reproduced.