ElementTreeを使ってパースしてみた
PythonでXMLをパースする機会があったので、色々と弄ってみました。
まず、PythonでXMLをパースする際には、ElementTreeというライブラリを使用すると簡単そうだったので、こちらを利用して進めました。
使用するデータは、以下のようなものを想定しています。
<?xml version='1.0' encoding='utf-8'?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<dc:creator>john</dc:creator>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
<dc:creator>dave</dc:creator>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<dc:creator>alex</dc:creator>
</country>
</data>
</rss>
ElementTreeを使用して、XMLをツリー構造化します。
まず、ElementTreeを使用して、XMLをツリー構造化します。
from xml.etree.ElementTree import parse, fromstring
from xml.parsers.expat import ExpatError
# ファイル名を指定する場合
try:
tree = parse('sample.xml')
except ExpatError:
self.fail("Unable to parse XML data from string")
# xmlを直接渡す場合
try:
tree = fromstring('<?xml version="1.0"?>;....')
except ExpatError:
self.fail("Unable to parse XML data from string")
ツリー構造化したobjectからタグを探す
続いて、ツリー構造化したものの中から、タグを探します。
今回の場合、dataの中にcountryが複数ある為、iterateしています。
for country in tree.find('data').getiterator('country'):
# タグ内のテキストを取得
country.find('rank').text
# 属性値を取得
country.find('neighbor').attrib['name']
# prefixの付いた値を取得
country.find("{http://purl.org/dc/elements/1.1/}" + 'creator').text
また、iterateせず、リストに入れた後に配列のindexを指定して、値を取得する方法もあります。
countries = tree.findall('data/country')
# 上記と同じように
for country in countries:
# タグ内のテキストを取得
country.find('rank').text
# 属性値を取得
country.find('neighbor').attrib['name']
# prefixの付いた値を取得
country.find("{http://purl.org/dc/elements/1.1/}" + 'creator').text
# index番号指定して、リスト内の値取得
countries[1].find('rank').text
countries[1].find('neighbor').attrib['name']










意外と、prefixを用いたdcの値を取得するのが大変でした。prefixが付いていると、ちょっと特殊な形として扱わないといけないんですね。