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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With