Parsing complex Xml Python 3.4 -


i have following xml :

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <suite> <testcase>   <testcaseid>001</testcaseid>   <testcasedescription>hello</testcasedescription>   <testsetup>     <action>       <actioncommand>gfdg</actioncommand>       <timeout>dfgd</timeout>       <bamsymbol>gff</bamsymbol>       <side>vfbgc</side>       <primebroker>fgfd</primebroker>       <size>fbcgc</size>       <pmcode>fdgd</pmcode>       <strategy>fdgf</strategy>       <substrategy>fgf</substrategy>       <actionlogendpoint>fdgf</actionlogendpoint>       <isactionresultlogged>fdgf</isactionresultlogged>       <validationstep>         <isvalidated>fgdf</isvalidated>         <validationformat>dfgf</validationformat>         <responseendpoint>gdf</responseendpoint>         <responseparametername>fdgfdg</responseparametername>         <responseparametervalue>gff</responseparametervalue>         <expectedvalue>fdgf</expectedvalue>         <isvalidationresultlogged>gdfgf</isvalidationresultlogged>         <validationlogendpoint>fdgf</validationlogendpoint>       </validationstep>     </action>     <action>       <actioncommand>new order</actioncommand>       <timeout>fdgf</timeout>       <bamsymbol>fdg</bamsymbol>       <side>c(cover)</side>       <primebroker>cspb</primebroker>       <size>fdgd</size>       <pmcode>gree</pmcode>       <strategy>generalist</strategy>       <substrategy>uslc</substrategy>       <actionlogendpoint>gfbhgf</actionlogendpoint>       <isactionresultlogged>fdgf</isactionresultlogged>       <validationstep>         <isvalidated>fdgd</isvalidated>         <validationformat>dfgfd</validationformat>         <responseendpoint>dfgf</responseendpoint>         <responseparametername>fdgfd</responseparametername>         <responseparametervalue>dfgf</responseparametervalue>         <expectedvalue>fdg</expectedvalue>         <isvalidationresultlogged>fdgdf</isvalidationresultlogged>         <validationlogendpoint>fdgfd</validationlogendpoint>       </validationstep>     </action>     </testcase> </suite> 

based on actioncommand getting either 1 block , issue not sub parent tag (validationstep) , child tags . can help?

my code:

for testsetup4 in root.findall(".testcase/testsetup/action"):      if testsetup4.find('actioncommand').text == "gfdg":          c1 in testsetup4:             t2.append(c1.tag)             v2.append(c1.text)           k,v in zip(t2, v2):             test_case[k] = v 

i not able validationstep (sub parent) , corresponding tags.

simply add loop iterate through <validationstep> node , children. also, not need 2 other lists can update dictionary during parsing loop:

import xml.etree.elementtree et  dom = et.parse('input.xml') root = dom.getroot()  test_case = {} testsetup4 in root.findall(".testcase/testsetup/action"):      if testsetup4.find('actioncommand').text == "gfdg":         c1 in testsetup4:             test_case[c1.tag]= c1.text         vd in testsetup4.findall("./validationstep/*"):             test_case[vd.tag]= vd.text 

alternatively, use double slash operator search children including grandchildren of <action> element:

for testsetup4 in root.findall(".testcase/testsetup/action"):      if testsetup4.find('actioncommand').text == "gfdg":          c1 in testsetup4.findall(".//*"):             test_case[c1.tag]= c1.text 

Comments