On Mon, May 7, 2012 at 11:50 AM, Vlad K. <[email protected]> wrote:

> 1. build XML in smaller chunks to a tempfile, yield it back to client


I have taken this approach in the past. Indeed if you use
tempfile.TemporaryFile (
http://docs.python.org/library/tempfile.html#tempfile.TemporaryFile) this
will work as you expect. In my particular case, I have the database
generate the xml row-wise (i.e. if my structure is
<records><record>...</record><record>...</record></records>) I have it
generate a bunch of <record>...</record> strings a the rows selected out of
the database. You can get the same effect with ElementTree (not sure about
lxml because it has extra constraints) by doing (with more error checking):

fd = tempfile.TemporaryFile()
fd.write('<records>')
while True:
   records = []
   rows = cursor.fechmany(2000)
   if not rows:
       break

   for row in rows:
      record =  ET.Element('record')
      ET.SubElement(record, 'somechild').text = row.data
      records.append(ET.tostring(record))

    fd.write("".join(records))

fd.write('</records>')

note that the 2000 in the fetchmany should be modified to fit your memory
and performance requirements.

I have also seen mention of several generation only XML libraries, but
can't remember which ones, I also have not used any of them.

-Chris

-- 
Christopher Lambacher
[email protected]

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to