Abhishek Kaushik
Abhishek Kaushik

Reputation: 93

Problem in parsing the XML file: xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 19, column 175

I need to parse an XML file with multiple roots but I am unable to read the file. I get an error

Traceback (most recent call last):
  File "C:/Users/Abhi/PycharmProjects/Trec_project/Index_with_Xml.py", line 34, in <module>
    root = ET.fromstringlist(complete)
  File "C:\Users\Abhi\Anaconda3\lib\xml\etree\ElementTree.py", line 1355, in fromstringlist
    parser.feed(text)
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 19, column 175

The same code is working for other XML files. I change the encoding style but it didn't work. I need to parse each element of the file. The data has multiple roots.

Here is the sample data

<DOC>
<DOCNO>WAPO_b2e89334-33f9-11e1-825f-dabc29fd7071-1</DOCNO>
<DOCHDR>
https://www.washingtonpost.com/sports/colleges/danny-coale-jarrett-boykin-are-a-perfect-1-2-punch-for-virginia-tech/2011/12/31/gIQAAaW4SP_story.html
</DOCHDR>
<HTML>
<BODY>
<span class="dateline">NEW ORLEANS —</span> Whenever a <a href="http://www.washingtonpost.com/blogs/hokies-journal" title="www.washingtonpost.com">Virginia Tech</a> offensive coach is asked how the most prolific receiving duo in school history came to be, inevitably the first road game in 2008 against North Carolina comes up.
</BODY>
</HTML>
</DOC>
<DOC>
<DOCNO>WAPO_b2e89334-33f9-11e1-825f-dabc29fd7071-2</DOCNO>
<DOCHDR>
https://www.washingtonpost.com/sports/colleges/danny-coale-jarrett-boykin-are-a-perfect-1-2-punch-for-virginia-tech/2011/12/31/gIQAAaW4SP_story.html
</DOCHDR>
<HTML>
<BODY>
Midway through the first quarter, Virginia Tech had to call two timeouts in a row because then-freshmen <a href="http://stats.washingtonpost.com/cfb/players.asp?id=168641&team=16" title="stats.washingtonpost.com">Jarrett Boykin</a> and <a href="http://stats.washingtonpost.com/cfb/players.asp?id=155812&team=16" title="stats.washingtonpost.com">Danny Coale</a> couldn’t seem to line up right, and “they had those big eyes out there looking around,” Kevin Sherman, their position coach, said recently.
</BODY>
</HTML>
</DOC>
<DOC>
<DOCNO>WAPO_b2e89334-33f9-11e1-825f-dabc29fd7071-3</DOCNO>
<DOCHDR>
https://www.washingtonpost.com/sports/colleges/danny-coale-jarrett-boykin-are-a-perfect-1-2-punch-for-virginia-tech/2011/12/31/gIQAAaW4SP_story.html
</DOCHDR>
<HTML>
<BODY>
Now that Boykin and Coale have only Tuesday’s Sugar Bowl remaining before leaving Virginia Tech with every major school record for a wide receiver, they’ve taken a different stance.
</BODY>
</HTML>
</DOC>
<DOC>
<DOCNO>WAPO_b2e89334-33f9-11e1-825f-dabc29fd7071-4</DOCNO>
<DOCHDR>
https://www.washingtonpost.com/sports/colleges/danny-coale-jarrett-boykin-are-a-perfect-1-2-punch-for-virginia-tech/2011/12/31/gIQAAaW4SP_story.html
</DOCHDR>
<HTML>
<BODY>
“I still don’t think that was on us. Macho [Harris] was in the game and he lined up wrong,” said Boykin, as Coale sat next to him nodding in agreement.
</BODY>
</HTML>
</DOC>

The code

import xml.etree.ElementTree as ET
# import xml.etree.cElementTree as ET

with open(path, encoding='utf-8-sig',errors='ignore') as f:
    #it = itertools.chain('<root>', f, '</root>')
    data=f.read()
    complete="<z>" + data + "</z>"
    #fixed = it.replace(b'\x0c', b'')
    root = ET.fromstringlist(complete)

# Do something with `root`
for x in root:
    print(x[0].text)
    print(x[2].text)

Upvotes: 0

Views: 870

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

If it has multiple roots then it's not an XML file. Or at least, to be technical, it's not a well-formed XML document entity.

It is however a well-formed XML external parsed entity, and that gives you a workaround. In fact it gives you a choice of two:

(a) read the contents of the file as a string, wrap it in a new outermost element ("<z>" + content + "</z>") and then parse that

(b) write a wrapper document that references the document as an external entity, and parse the wrapper document:

<!DOCTYPE z [
<!ENTITY e SYSTEM "content.xml">
]>
<z>&e;</z>

Upvotes: 1

Related Questions