Parsing xml file using python

Peter Hansen peter at engcorp.com
Fri Mar 5 10:13:18 EST 2004


chad wrote:

> I am new to Python.

And XML?

> 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.

Note that what you have above is _not_ an XML file.  That is, it looks 
like XML but it's not well-formed, as it doesn't have a single enclosing 
  element.

You probably meant that just as a quickie example, but in case that's 
like what your actual data format looks like, you'll have trouble using 
any of the Python XML parsers.  (The fix is pretty trivial though.)

-Peter



More information about the Python-list mailing list