2011-10-31 9 views
3

接頭辞がある場合を除いて、私はタグを読むことができます。私は前の質問のためにSOを捜しているわけではありません。接頭辞のタグ付き解析.xml? xml.etree.ElementTree

私はmedia:contentを読む必要があります。私はimage = node.find("media:content")を試しました。 のRss入力:

<channel> 
    <title>Popular Photography in the last 1 week</title> 
    <item> 
    <title>foo</title> 
    <media:category label="Miscellaneous">photography/misc</media:category> 
    <media:content url="http://foo.com/1.jpg" height="375" width="500" medium="image"/> 
    </item> 
    <item> ... </item> 
</channel> 

私は兄弟タグtitleを読むことができます。

from xml.etree import ElementTree 
with open('cache1.rss', 'rt') as f: 
    tree = ElementTree.parse(f) 

for node in tree.findall('.//channel/item'): 
    title = node.find("title").text 

私は、ドキュメントを使用して、まだ「プレフィックス」の部分に貼り付けてきました。

答えて

4

ElementTreeのでXML名前空間を使用しての例です:

>>> x = '''\ 
<channel xmlns:media="http://www.w3.org/TR/html4/"> 
    <title>Popular Photography in the last 1 week</title> 
    <item> 
    <title>foo</title> 
    <media:category label="Miscellaneous">photography/misc</media:category> 
    <media:content url="http://foo.com/1.jpg" height="375" width="500" medium="image"/> 
    </item> 
    <item> ... </item> 
</channel> 
''' 
>>> node = ElementTree.fromstring(x) 
>>> for elem in node.findall('item/{http://www.w3.org/TR/html4/}category'): 
     print elem.text 


photography/misc 
関連する問題