Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ParseError: undefined entity while parsing XML file in Python

I have a big XML file with several article nodes. I have included only one with the problem. I try to parse it in Python to filter some data and I get the error

File "<string>", line unknown
ParseError: undefined entity &Ouml;: line 90, column 17

Sample of the XML file

<?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE dblp SYSTEM "dblp.dtd">
    <dblp>
        <article mdate="2019-10-25" key="tr/gte/TR-0146-06-91-165" publtype="informal">
            <author>Alejandro P. Buchmann</author>
            <author>M. Tamer &Ouml;zsu</author>
            <author>Dimitrios Georgakopoulos</author>
            <title>Towards a Transaction Management System for DOM.</title>
            <journal>GTE Laboratories Incorporated</journal>
            <volume>TR-0146-06-91-165</volume>
            <month>June</month>
            <year>1991</year>
            <url>db/journals/gtelab/index.html#TR-0146-06-91-165</url>
        </article>
    </dblp>

From my search in Google, I found that this kind of error appears if you have issues in the node names. However, the line with the error is the second author, in the text.

This is my Python code

with open('xaa.xml', 'r') as xml_file:
    xml_tree = etree.parse(xml_file)
like image 329
Tasos Avatar asked Oct 21 '25 15:10

Tasos


1 Answers

The declaration of the Ouml entity is presumably in the DTD (dblp.dtd), but ElementTree does not support external DTDs. ElementTree only recognizes entities declared directly in the XML file (in the "internal subset"). This is a working example:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE dblp [
<!ENTITY Ouml 'Ö'>
]>
<dblp>
  <article mdate="2019-10-25" key="tr/gte/TR-0146-06-91-165" publtype="informal">
    <author>Alejandro P. Buchmann</author>
    <author>M. Tamer &Ouml;zsu</author>
    <author>Dimitrios Georgakopoulos</author>
    <title>Towards a Transaction Management System for DOM.</title>
    <journal>GTE Laboratories Incorporated</journal>
    <volume>TR-0146-06-91-165</volume>
    <month>June</month>
    <year>1991</year>
    <url>db/journals/gtelab/index.html#TR-0146-06-91-165</url>
  </article>
</dblp>

To parse the XML file in the question without errors, you need a more powerful XML library that supports external DTDs. lxml is a good choice for that.

like image 125
mzjn Avatar answered Oct 23 '25 07:10

mzjn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!