2012-06-27 21 views
15

elementtree.ElementTreeの名前のXML名前空間を無視する方法はありますか?Python:elementtree.ElementTreeのxmlnsを無視する

私はすべてのtechnicalContactタグ印刷しよう:

for item in root.getiterator(tag='{http://www.example.com}technicalContact'): 
     print item.tag, item.text 

をそして私のようなものを得る:

{http://www.example.com}technicalContact [email protected] 

をしかし、私が本当にしたいことは次のとおりです。

technicalContact [email protected] 

は、方法はありますサフィックス(sans xmlns)のみを表示するか、明示的にxmlnsを記述しなくても要素を繰り返し処理することができますか?

+0

多くのためhttp://stackoverflow.com/a/25920989/2593383下私の答えを参照してください。一般的な解決策 – nonagon

答えて

8

のようなものを使用して終わります。例えば、このような何か:

def get_element_by_tag(element, tag): 
    if element.tag.endswith(tag): 
     yield element 
    for child in element: 
     for g in get_element_by_tag(child, tag): 
      yield g 

これはちょうど、すなわち、先頭の名前空間を無視して、tagで終わるタグをチェックします。次のようにあなたは、あなたが望む任意のタグを反復処理することができます

for item in get_element_by_tag(elemettree, 'technicalContact'): 
    ... 

このジェネレータをアクションに:

>>> xml_str = """<root xmlns="http://www.example.com"> 
... <technicalContact>Test1</technicalContact> 
... <technicalContact>Test2</technicalContact> 
... </root> 
... """ 

xml_etree = etree.fromstring(xml_str) 

>>> for item in get_element_by_tag(xml_etree, 'technicalContact') 
...  print item.tag, item.text 
... 
{http://www.example.com}technicalContact Test1 
{http://www.example.com}technicalContact Test2 
+0

うまくいけば、上記の質問に答えます。私が気づいた違いは、ジェネレータの例の 'item'に' next'メソッドがないことです。それでも、これ以外は 'etree.getiterator'と同じ(似たような)方法で動作します。 – Chris

0

私はいつもあなたが再帰的に適切なタグ名と終了タグを見つけるためにあなたの要素ツリーを検索するジェネレータを定義することができ

item.tag.split("}")[1][0:] 
+0

iteratorの問題には対処できません - 私はまだ完全なタグ名を反復処理する必要があります。 –

+0

私はそれを行うpythonのための異なったxmlハンドラを知らない。 lxmlを使用すると、解析する前にXMLでxlstを使用できます。 – lebox

+2

'[0:]'とは何ですか? – jadkik94

関連する問題