Parsing xml file using python

Sam Smith nikita_raja at
Fri Mar 5 22:02:00 CET 2004

Please try the following code.  As Tim says everything you need for
your problem at hand is included in the core distribution.  Look for
documentation for xml.sax module.

Code ::

 import xml.sax.handler
 import xml.sax
 import sys

 class ReadXML(xml.sax.handler.ContentHandler):
    def __init__(self, xml_file):
        self.content = ""                
        xml.sax.parse(xml_file, self)

    def characters(self, content):
        self.content = self.content.lstrip().rstrip() + " " + content
 if __name__=="__main__":
    if len(sys.argv) != 3:
        print "usage: %prog xml_file output_file"
    xml_file = sys.argv[1]
    output_file = sys.argv[2]    
    readXML = ReadXML(xml_file)
    f = file(output_file, "w+")

More information about the Python-list mailing list