Parsing xml file using python

Georgy no.mail at available.net
Fri Mar 5 10:50:36 EST 2004


There's standard xml modules with built-in SAX2 parser.

http://pyxml.sourceforge.net/topics/howto/xml-howto.html
http://www.python.org/doc/current/lib/markup.html
http://www.devarticles.com/c/a/Python/Parsing-XML-with-SAX-and-Python/2/

For more samples: http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=xml+python+sample&btnG=Google+Search

And the code you're looking for will be like this (not tested):

import sys
from xml.sax import make_parser, handler
class BodyOnly(handler.ContentHandler):
  def characters( self, content ):
    print content,
parser = make_parser()
parser.setContentHandler(BodyOnly())
parser.parse( "input.xml" )



"chad" <antonyliu2002 at yahoo.com> wrote in message news:14b36d18.0403041649.252d2e7c at posting.google.com...
| Hello, all,
|
| I am new to Python.
|
| I need to read an XML document and ignore all XML tags and write only
| those between the tags to a text file.  In other words, if I have an
| XML document like so:
|
| <tag1>This</tag1>
|     <tag2>is</tag2>
|        <tag3>a</tag3>
| <tag1>test</tag1>
|
| I need to write "This is a test" to a text file.  How do I achieve
| this?    Thanks.





More information about the Python-list mailing list