Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why invoking ElementTree.parse function twice in one script will get an exception?

I need to process two xml files in one script. So, I wrote some codes like below:

import xml.etree.cElementTree as ET

parser = ET.XMLParser(encoding='utf-8')
ET.parse('../wiki.xml', parser=parser)
ET.parse('../tutorial.xml', parser=parser)

However, the second call parse got an exception.

cElementTree.ParseError: parsing finished

But, I ensure that both xml files have no inner problems, because when I changed the order the second invoke always got an exception and if I reserved one there was no problem.

So, why invoking ElementTree.parse function twice in one script will get an exception?

like image 644
Qinghua Avatar asked Oct 27 '25 20:10

Qinghua


1 Answers

You're supposed to create new parser for each tree. Because parser holds parsed state inside, it can't be used to parse another tree.

import xml.etree.cElementTree as ET

parser1 = ET.XMLParser(encoding='utf-8')
ET.parse('../wiki.xml', parser=parser1)
parser2 = ET.XMLParser(encoding='utf-8')
ET.parse('../tutorial.xml', parser=parser2)
like image 126
Yaroslav Admin Avatar answered Oct 30 '25 11:10

Yaroslav Admin