Published On: 1970-01-01|Last Updated: 1970-01-01|Categories: Uncategorized|
正直どうかと思うよ。

きちんとしてない XML の解析は BeautifulSoup でやる方が楽。

インポート

Python 2.5 からは標準モジュールになったので場所が違うらしい。

try:
from xml.etree import ElementTree
except:
from elementtree import ElementTree

文字列から解析

root = ElementTree.fromstring(contents)

エレメント

  1. 真偽値は常に False になるっぽい
#タグ名 しばしば名前空間が付くので使いにくい
e.tag
#属性
e.attrib
#中身
e.text
#子エレメント
e.getChildren()
#すべて列挙
e.getiterator()
#検索 XPathのサブセット
e.find(path), e.findall(path)

名前空間をいじる

http://blogs.nuxeo.com/sections/blogs/julien_anguenot/2006_02_23_elementtree-serialization-namespace-prefixes

関連