473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

xml.dom.minidom memory usage

Dan
I'm using python's xml.dom.minidom module to generate xml files, and
I'm running into memory problems. The xml files I'm trying to create
are relatively flat, with one root node which may have millions of
direct child nodes.

Here's an example script:
#!/usr/bin/env python
import xml.dom.minidom
def gen_xml(n):
doc = xml.dom.minidom.Document()
root = xml.dom.minidom.Element("foo")
root.ownerDocument = doc
root.setAttribute("one", "1")
doc.appendChild(root)
for x in xrange(n):
elem = xml.dom.minidom.Element("bar")
elem.ownerDocument = doc
elem.setAttribute("attr1", "12345678")
elem.setAttribute("attr2", "87654321")
root.appendChild(elem)
return doc
if I run gen_xml(1000000), my python process ends up using all my 90%
of my memory, and the system ends up thrashing (Linux, P4, 1G ram,
python 2.4.3) .

So, my questions are (1) am I doing something dumb in the script that
stops python from collecting temp garbage? (2) If not, is there
another reasonable module to generate xml (as opposed to parsing it),
or should I just implement my own xml generation solution?

Thanks,
-Dan

Feb 1 '07 #1
6 4448
Dan,
The DOM (Document Object Model) is such that it loads all the elements of the
XML document into memory before you can do anything with it. With your file
containing millions of child nodes this will eat up as much memory as you
have. A solution to this is to use the SAX method of parsing XML documents
and working on it. SAX is such that it only reads the XML doc. a node (or a
few nodes) at a time.

Unfortunately, the use of DOM or SAX completely depends on what kind of
processing you need to be done on the XML document. If it is editing a record
at a time (from what I've gathered from your code) it would be wise to use
SAX. I highly suggest looking into this method of processing.

- Jonathan Curran
Feb 1 '07 #2
Dan,
I jumped the gun and didn't read your entire post. I ended up skipping a lot
of parts, especially where you say that you are creating a document (for some
reason I thought you were reading it). I looked at the setAttribute function
and thought: ah he's writing it out.
Sorry about all that. I'd still ask you to look into SAX though :) Especially
when dealing with really large XML documents, whether it be reading or
writing them.

- Jonathan Curran
Feb 1 '07 #3
Dan
On Feb 1, 3:12 pm, Jonathan Curran <j...@icicled.netwrote:
Dan,
The DOM (Document Object Model) is such that it loads all the elements of the
XML document into memory before you can do anything with it. With your file
containing millions of child nodes this will eat up as much memory as you
have. A solution to this is to use the SAX method of parsing XML documents
and working on it. SAX is such that it only reads the XML doc. a node (or a
few nodes) at a time.

Unfortunately, the use of DOM or SAX completely depends on what kind of
processing you need to be done on the XML document. If it is editing a record
at a time (from what I've gathered from your code) it would be wise to use
SAX. I highly suggest looking into this method of processing.

- Jonathan Curran
Jonathan,

Thanks for the response. I'm comfortable using SAX for parsing, but
I'm unsure how it helps me with XML generation. To clarify in my
example code, with the DOM document, I can call doc.toxml() or
doc.toprettyxml(), and get the desired output (an xml file). AFAIK,
SAX has no analog, it just does parsing. Is there a way to do XML
generation with SAX that I'm unaware of?

-Dan

Feb 1 '07 #4
Dan a écrit :
I'm using python's xml.dom.minidom module to generate xml files, and
I'm running into memory problems. The xml files I'm trying to create
are relatively flat, with one root node which may have millions of
direct child nodes.
Woops ! You're looking for trouble.
>
So, my questions are (1) am I doing something dumb in the script
Yes : using minidom !-)
that
stops python from collecting temp garbage?
That's not the problem. The problem is with how xml dom APIs work: they
build the whole damn tree in memory.
(2) If not, is there
another reasonable module to generate xml (as opposed to parsing it),
or should I just implement my own xml generation solution?
You should have a look at Genshi:
http://genshi.edgewall.org/

Feb 1 '07 #5
Dan,
Take a look at http://www.xml.com/pub/a/2003/03/12/py-xml.html. It's a
starting point to output XML data via use of SAX. Bruno also mentioned
Genshi. I haven't used Genshi myself, but it'd be worth it to take a look at
what it has to offer.

- Jonathan
Feb 1 '07 #6
Dan wrote:
I'm using python's xml.dom.minidom module to generate xml files, and
I'm running into memory problems.
Then take a look at cElementTree. It's part of Python 2.5 and is available as
a separate module for Python 2.4. It's fast, has a very low memory profile and
if you ever decide to need more features, there's lxml to the rescue.

You might also consider streaming your XML piece by piece instead of creating
in-memory trees. Python's generators are a good starting point here.

Stefan
Feb 18 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Paul Miller | last post by:
We've run into minidom's inabilty to handle large (20+MB) XML files, and need a replacement that can handle it. Unfortunately, we're pretty dependent on a DOM, so a pulldom or SAX replacement is...
0
by: xtian | last post by:
Hi - I'm doing some data conversion with minidom (turning a csv file into a specific xml format), and I've hit a couple of small problems. 1: The output format has a header with some xml that...
5
by: Mike McGavin | last post by:
Hi everyone. I've been trying for several hours now to get minidom to parse namespaces properly from my stream of XML, so that I can use DOM methods such as getElementsByTagNameNS(). For some...
4
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3...
18
by: sim.sim | last post by:
Hi all. i'm faced to trouble using minidom: #i have a string (xml) within CDATA section, and the section includes "\r\n": iInStr = '<?xml version="1.0"?>\n<Data><!]></Data>\n' #After i...
0
by: Gary | last post by:
Howdy I ran into a difference between Python on Windows XP and Linux Fedora 6. Writing a dom to xml with minidom works on Linux. It gives an error on XP if there is an empty namespace. The...
3
by: aine_canby | last post by:
Hi, I'm working with a number of scripts which were written years ago for my company for Python 2.2, and I'd like to update for Python 2.5. I have written a script to add # -*- coding: cp1252...
0
by: sweavo | last post by:
Hi all, (Python 2.5 under cygwin) I'm reading a bunch of XML files and merging them in memory - each file contains a number of packages which are to be merged: for fname in gInfiles: ...
1
by: Jean-Paul Calderone | last post by:
On Tue, 22 Apr 2008 14:54:37 -0700 (PDT), yzghan@gmail.com wrote: The test doesn't demonstrate any leaks. It does demonstrate that memory usage can remain at or near peak memory usage even after...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.