0
我有一個需求,我需要解析XML並需要捕獲整個內容到字符串。我嘗試使用minidom,它是可以的。請幫助受ElementTree的Python ElementTree xml內容作爲字符串
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>
minidom命名實例:
from xml.dom import minidom
xmldoc = minidom.parse(country.xml)
print xmldoc.toxml()
然而,當我使用它的ElementTree不知道如何可以做到這一點做的:我的代碼到目前爲止
import xml.etree.ElementTree as etree
tree = etree.parse('C:\\Users\\Administrator\\Desktop\\country.xml')
print tree.getroot() *This is not working*