Parsing xml file using python
Sam Smith
nikita_raja at yahoo.com
Fri Mar 5 16:02:00 EST 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):
xml.sax.handler.ContentHandler.__init__(self)
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"
sys.exit()
xml_file = sys.argv[1]
output_file = sys.argv[2]
readXML = ReadXML(xml_file)
f = file(output_file, "w+")
f.write(readXML.content)
f.close()
More information about the Python-list
mailing list