我有一个XML文件,需要提取其中一些标签以便后续使用,这些标签包含如下数据:
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
<?xml version="1.0"?>
<data>
<country name="Liechtenstein1">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria1" direction="E"/>
<neighbor name="Switzerland1" direction="W"/>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia1" direction="N"/>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
我需要解析这个,所以我使用了:
import xml.etree.ElementTree as ET
tree = ET.parse("myfile.xml")
root = tree.getroot()
这段代码在第二行报错:xml.etree.ElementTree.ParseError: junk after document element:
我认为是由于有多个XML标签导致的,你有什么建议吗?应该如何解析它?
<?xml version="1.0"?>
之前将其拆分,并单独解析这些部分。 - Klaus D.