2015-03-19 160 views
4

我正在使用Python解析XML(xml.dom.minidom),並且無法獲取節點的tagName。獲取element.tagName的問題。使用Python和xml.dom.minidom解析XML

解釋器將返回:

AttributeError: Text instance has no attribute 'tagName' 

當我嘗試從節點提取物(例如)字符串「格式」:

<format>DVD</format> 

我已經發現一對夫婦非常相似職位在Starckoverflow中,但我仍然找不到解決方案。

我知道可能有替代模塊來解決這個問題,但我的意圖是理解爲什麼它失敗。

非常感謝提前和問候,

這裏是我的代碼:

from xml.dom.minidom import parse 
import xml.dom.minidom 

# Open XML document 
xml = xml.dom.minidom.parse("movies.xml") 

# collection Node 
collection_node = xml.firstChild 

# movie Nodes 
movie_nodes = collection_node.childNodes 

for m in movie_nodes: 

    if len(m.childNodes) > 0: 
     print '\nMovie:', m.getAttribute('title') 

     for tag in m.childNodes: 
      print tag.tagName # AttributeError: Text instance has no attribute 'tagName' 
      for text in tag.childNodes: 
       print text.data 

而且這裏的XML:

<collection shelf="New Arrivals"> 
<movie title="Enemy Behind"> 
    <type>War, Thriller</type> 
    <format>DVD</format> 
    <year>2003</year> 
    <rating>PG</rating> 
    <stars>10</stars> 
    <description>Talk about a US-Japan war</description> 
</movie> 
<movie title="Transformers"> 
    <type>Anime, Science Fiction</type> 
    <format>DVD</format> 
    <year>1989</year> 
    <rating>R</rating> 
    <stars>8</stars> 
    <description>A schientific fiction</description> 
</movie> 
</collection> 

類似的帖子:

Get node name with minidom

Element.tagName for python not working

回答

6

錯誤是由於元件節點之間的新的行被視爲類型TEXT_NODE(見Node.nodeType),和TEXT_NODE的不具有tagName屬性不同的節點。

您可以添加一個節點類型檢查,以避免文本節點打印tagName

if tag.nodeType != tag.TEXT_NODE: 
    print tag.tagName 
+0

解決!非常感謝! – MAAT 2015-03-19 13:54:35

0

這是怎樣的代碼看起來像由用戶上面提出的修改:har07

for tag in m.childNodes: 
     if tag.nodeType != tag.TEXT_NODE: 
     for text in tag.childNodes: 
      print tag.tagName, ':', text.data 

它現在就像一個魅力。