Having problems getting data out of an xml element in Python -
i parsing xml output program.
here's example of xml fragment:
<result test="passed" stamp="2011-01-25t12:40:46.166-08:00"> <assertion>multipletesttool1</assertion> <comment>multipletesttool1 passed</comment> </result>
i want data out of <comment>
element.
here code snippet:
import xml.dom.minidom mydata.cnodes = mydata.rnode.getelementsbytagname("comment") value = self.getresultcommenttext( mydata.cnodes def getresultcommenttext(self, nodelist): rc = [] node in nodelist: if node.nodename == "comment": if node.nodetype == node.text_node: rc.append(node.data) return ''.join(rc)
value empty, , appears nodetype element_node, .data
doesn't exist new python, , causing me scratch head. can tell me i'm doing wrong?
try elementtree instead of minidom:
>>> import xml.etree.celementtree 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' >>>
Comments
Post a Comment