Python xml ElementTree APIを使用して以下のXMLファイルを解析したいと思います。このサンプルコードでPython xml ElementTree findallは空の結果を返します
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foos>
<foo_table>
<!-- bar -->
<fooelem>
<fname>BBBB</fname>
<group>SOMEGROUP</group>
<module>some module</module>
</fooelem>
<fooelem>
<fname>AAAA</fname>
<group>other group</group>
<module>other module</module>
</fooelem>
<!-- bar -->
</foo_table>
</foos>
私は/ FOOS/foo_table/fooelem/fnameの下にあるすべての要素を見つけることを試みるが、このコードを実行するとき、明らかにfindAllは何かを見つけることはありません。
import xml.etree.cElementTree as ET
tree = ET.ElementTree(file="min.xml")
for i in tree.findall("./foos/foo_table/fooelem/fname"):
print i
root = tree.getroot()
for i in root.findall("./foos/foo_table/fooelem/fname"):
print i
私はElementTreeのAPIで経験していないのですが、私はhttps://docs.python.org/2/library/xml.etree.elementtree.html#example下の例を使用しました。私の場合、なぜそれがうまくいかないのですか?
ありがとうございます。 tree.findall()の代わりにroot.findall()を選択する特別な理由はありますか?両方とも同じ結果を示しているようです。 – ThoWe
@ThoWe:私が知っている理由はありません、私の推測では、それはちょうど慣習であるということでしょう。 –