2011-01-25 121 views
0

我解析了另一個程序輸出的xml。從Python中的xml元素獲取數據時出現問題

這裏的XML片段的例子:

<result test="Passed" stamp="2011-01-25T12:40:46.166-08:00"> 
     <assertion>MultipleTestTool1</assertion> 
     <comment>MultipleTestTool1 Passed</comment> 
     </result> 

我想要得到的數據了<comment>元素。

這裏是我的代碼片段:

import xml.dom.minidom 
mydata.cnodes = mydata.rnode.getElementsByTagName("comment")       
    value = self.getResultCommentText(mydata.cnodes 

    def getResultCommentText(self, nodelist): 
      rc = [] 
      for node in nodelist: 
       if node.nodeName == "comment": 
        if node.nodeType == node.TEXT_NODE: 
         rc.append(node.data) 

     return ''.join(rc) 

值始終是空的,看來該節點類型始終是一個ELEMENT_NODE,所以.data不存在我是新來的Python,這也是造成我撓撓我的腦袋。誰能告訴我我做錯了什麼?

回答

0

你在這裏:

>>> from lxml import etree 
>>> result = """ 
... <result test="Passed" stamp="2011-01-25T12:40:46.166-08:00"> 
...   <assertion>MultipleTestTool1</assertion> 
...   <comment>MultipleTestTool1 Passed</comment> 
...  </result> 
... """ 
>>> xml = etree.fromstring(result) 
>>> xml.xpath('//comment/text()') 
['MultipleTestTool1 Passed'] 
>>> 
1

嘗試,而不是minidom命名的ElementTree:

>>> import xml.etree.cElementTree as et 
>>> data = """ 
... <result test="Passed" stamp="2011-01-25T12:40:46.166-08:00"> 
...   <assertion>MultipleTestTool1</assertion> 
...   <comment>MultipleTestTool1 Passed</comment> 
...  </result> 
... """ 
>>> root = et.fromstring(data) 
>>> root.tag 
'result' 
>>> root[0].tag 
'assertion' 
>>> root[1].tag 
'comment' 
>>> root[1].text 
'MultipleTestTool1 Passed' 
>>> root.findtext('comment') 
'MultipleTestTool1 Passed' 
>>> 
0

繼續使用minidom命名,我修改您的代碼段,指示所需方法:

import xml.dom.minidom 
mydata.cnodes = mydata.rnode.getElementsByTagName("comment") 
value = self.getResultCommentText(mydata.cnodes) 
    def getResultCommentText(self, nodelist): 
    rc = [] 
    for node in nodelist: 
     # Since the node list was created by getElementsByTagName("comment"), 
     # all nodes in this list will be comment nodes. 
     # 
     # The text data required is a child of the current node 
     for child in node.childNodes: 
     # If the current node is a text node, append it's information 
     if child.nodeType == child.TEXT_NODE: 
      rc.append(child.data) 
    return ''.join(rc) 

基本上,所發生的是所需的文本數據包含在文本節點中是評論節點的孩子。首先,必須檢索節點,然後可以檢索數據。