2011-01-25 73 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) 

基本上,所发生的是所需的文本数据包含在文本节点中是评论节点的孩子。首先,必须检索节点,然后可以检索数据。