[XML-SIG] building XML docs using ?
Fred L. Drake, Jr.
fdrake@acm.org
Mon, 14 May 2001 15:23:57 -0400 (EDT)
Joe Murray writes:
> Currently, I parse through the text files and create a DOM Document
> representation. However, the time and memory expenditure for conversion
> is huge, using either xml.dom.minidom or xml.dom. Here's an example of
> what I do:
Instead of building a DOM tree, send events to a SAX output
generator. This avoids keeping your entire document in memory. The
xml.sax.writer module provides this, and there may be others. (Be
sure to get the xml.sax.writer from CVS though; I just fixed a really
stupid bug...)
> ----------
>
> # import stuff
> from xml.dom.minidom import Document
>
> # create doc and documentElement node
> doc = Document()
> docelement = doc.appendChild(...)
> f = open(...)
> ..
> while 1:
>
> # get data from file
> line = f.readline()
> if not line:
> break
> line = line.strip()
> data = line.split(...)
>
> # create a new element node using data from file
> node = doc.createElement(...)
> node.setAttribute(...)
> node.appendChild(...)
> docelement.appendChild(node)
This would end up looking more like:
writer = xml.sax.writer.XmlWriter(f)
while 1:
# get data from file
...
# write new element to output:
writer.startElement("item", {"attr": value})
writer.characters(data)
writer.endElement("item")
writer.characters("\n") # record separator, unless you're
# using the PrettyPrinter version
f.close()
> So basically, is there a lightweight XML module which provides for (as a
> graphics programmer would say) "immediate mode" output, with as nice an
> interface as the DOM modules? Oh, and BTW, can XML solve all my
> problems??? ;-)
XML is an acronym, and as everyone knows, acronyms solve problems.
All of them. So, yes, life will be perfect with your new-found TLA. ;)
-Fred
--
Fred L. Drake, Jr. <fdrake at acm.org>
PythonLabs at Digital Creations