Reputation: 2279
I have this python code that accepts as input an XML file. When I run the execution, I get the error indicated in the title of the question:
for event,element in cElementTree.iterparse(io, events = ( b'start',b'end')):
if event == b'start':
self.TreeBuilder.start(element.tag, element.attrib)
error:
> File "/usr/lib/python2.7/site-packages/pymzml/run.py", line 370, in
> __init__
> self.TreeBuilder.start(element.tag, element.attrib)
cElementTree.ParseError: multiple elements on top level
help me to solve this problem! thank you.
Upvotes: 0
Views: 1845
Reputation: 17246
If I'm guessing correctly, it appears you are parsing an XML fragment with multiple root nodes while TreeBuilder is expecting a rooted document.
Try wrapping your parsing with calls to create a tree with a single root node. In other words:
self.TreeBuilder.start("root", {})
[...]
for event,element in cElementTree.iterparse(io, events = ( b'start',b'end')):
if event == b'start':
self.TreeBuilder.start(element.tag, element.attrib)
[...]
self.TreeBuilder.end("root")
Just remember that the resulting DOM has this extra element at the top and modify your processing to take that into account.
Upvotes: 1