Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ElementTree parsing unbound prefix error

Tags:

I am learning ElementTree in python. Everything seems fine except when I try to parse the xml file with prefix:

test.xml:

<?xml version="1.0"?>
<abc:data>
   <abc:country name="Liechtenstein" rank="1" year="2008">
   </abc:country>
   <abc:country name="Singapore" rank="4" year="2011">
   </abc:country>
   <abc:country name="Panama" rank="5" year="2011">
   </abc:country>
</abc:data>

When I try to parse the xml:

import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')

I got the following error:

xml.etree.ElementTree.ParseError: unbound prefix: line 2, column 0

Do I need to specify something in order to parse a xml file with prefix?

like image 429
Kintarō Avatar asked Nov 14 '12 03:11

Kintarō


1 Answers

Add the abc namespace to your xml file.

<?xml version="1.0"?>
<abc:data xmlns:abc="your namespace">
like image 169
Thiru Avatar answered Sep 19 '22 01:09

Thiru