XML Parsing: Expat Error

Gerth, William D gerthwd at rose-hulman.edu
Thu Jul 17 15:58:33 EDT 2008


Hey all, I'm simply trying to get my feet wet with XML parsing, and I
tried to just do something simple with ElementTree, just throw the XML
tags from a file into a list.  The code is as follows (and may be
wrong):

 

import glob

import xml.etree.ElementTree as ET

 

tree = ET.parse('nameofFilehere').getroot()

list = []

for branch in tree:

    this = {}

    for child in branch.getchildren():

        this[child.tag] = child.text

        list.append(this)

 

Every time I run the program, I get this error:

 

Traceback (most recent call last):

  File "nameofFilehere", line 4, in <module>

    coords = ET.parse('nameofFilehere').getroot()

  File "C:\Program Files\Python25\Lib\xml\etree\ElementTree.py", line
862, in parse

    tree.parse(source, parser)

  File "C:\Program Files\Python25\Lib\xml\etree\ElementTree.py", line
587, in parse

    self._root = parser.close()

  File "C:\Program Files\Python25\Lib\xml\etree\ElementTree.py", line
1254, in close

    self._parser.Parse("", 1) # end of data

xml.parsers.expat.ExpatError: no element found: line 3, column 0

 

What can I do to fix this, if anything?  My overall goal has been to
simply get the text of the XML document into a text file, but even that
has failed (I get naught but gibberish), so any help would be
appreciated.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080717/cdf251e3/attachment.html>


More information about the Python-list mailing list