tags:

views:

44

answers:

1

I know how to parse xml with sax in python, but how would I go about inserting elements into the document i'm parsing? Do I have to create a separate file?

Could someone provide a simple example or alter the one I've put below. Thanks.


from xml.sax.handler import ContentHandler
from xml.sax import make_parser
import sys

class aHandler(ContentHandler):

    def startElement(self, name, attrs):
        print "<",name,">"

    def characters(self, content):
        print content

    def endElement(self,name):
        print "</",name,">"


handler = aHandler()
saxparser = make_parser()
saxparser.setContentHandler(handler)

datasource = open("settings.xml","r")
saxparser.parse(datasource)

<?xml version="1.0"?>
<names>
    <name>
      <first>First1</first>
      <second>Second1</second>
    </name>
    <name>
      <first>First2</first>
      <second>Second2</second>
    </name>
    <name>
      <first>First3</first>
      <second>Second3</second>
    </name>
</names>
A: 

With DOM, you have the entire xml structure in memory.
With SAX, you don't have a DOM available, so you don't have anything to append an element to.

The main reason for using SAX is if the xml structure is really, really huge-- if it would be a serious performance hit to place the DOM in memory. If that isn't the case (as it appears to be from your small sample xml file), I would always use DOM vs. SAX.

If you go the DOM route, (which seems to be the only option to me), look into lxml. It's one of the best python xml libraries around.

ma3
I have simple example code because it would be easiest to learn from, but I intend to parse a very large (about 200MB) file.
usertest
In that case, the only way I could think to accomplish this would be for your SAX handler to capture every possible event, and repeat them back to another handler that creates a separate file. If you want to insert an element into a 200MB file and have it persist to disk, you'll somehow have to write to disk a 200MB file.
ma3