Aillyn
Aillyn

Reputation: 23783

Why is ElementTree.iterparse() raising a ParseError?

import xml.etree.ElementTree as ET
xmldata = file('my_xml_file.xml')

tree = ET.parse(xmldata)
root = tree.getroot()
root_iter = root.iter()

Now I can call root_iter.next() and get my Element objects. The problem is the real file I am working with is huge and I can't fit all of it in memory. So I am trying to use:

parse_iter = ET.iterparse(xmldata)

If I call parse_iter.next() it raises the following

Traceback (most recent call last):
  File "<pyshell#38>", line 1, in <module>
    parse_iter.next()
  File "C:\Python27\lib\xml\etree\ElementTree.py", line 1260, in next
    self._root = self._parser.close()
  File "C:\Python27\lib\xml\etree\ElementTree.py", line 1636, in close
    self._raiseerror(v)
  File "C:\Python27\lib\xml\etree\ElementTree.py", line 1488, in _raiseerror
    raise err
ParseError: no element found: line 1, column 0

What am I doing wrong?

Upvotes: 3

Views: 1887

Answers (1)

Aillyn
Aillyn

Reputation: 23783

The code I had was perfectly fine, except I was calling ElementTree.iterparse() on a file object I had already read with ElementTree.parse(). D'oh!

enter image description here

So for those who happen to make the same mistake, the solution is to either open a new file object or use file.seek(0) to reset the file cursor.

Upvotes: 3

Related Questions