[Tutor] I need speed-up [Generating XML with XMLGenerator]

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Dec 19 19:20:13 EST 2003



On Fri, 19 Dec 2003, Luiz Siqueira Neto wrote:

> I have a algorithm to make a XML document using as source a file with
> data like a memory layout, the algorithm work fine but get 100 MB of
> memory and is 18x more slow than the same algorithm using Delphi and
> MSXML.

Hi Luiz,


What is your algorithm?  How are you measuring performance?  Show us what
code you're using to generate things, and we can probably point out a few
things to help speed the code up.


If you're trying to generate XML, you may want to look at the XMLGenerator
class in the xml.sax.saxutils package:

    http://python.org/doc/lib/module-xml.sax.saxutils.html

(Just wondering: is anyone else having difficulty reaching Python.org, or
is it just me?)


Anyway, XmlGenerator is designed to help us generate XML files, and it
should be very resource efficient.  Furthermore, it does proper XML
quoting of character data.  Here's a small example:


###
"""A small example of XMLGenerator."""

from xml.sax.saxutils import XMLGenerator
from StringIO import StringIO
f = StringIO()
g = XMLGenerator(f)
g.startElement("FOOBAR", {})
g.characters("this is a <<test>>\n")
g.endElement("FOOBAR")
print f.getvalue()
###



Good luck to you!




More information about the Tutor mailing list