2016-07-21 8 views
0

ich folgende xml haben:Parsing komplexe XML-Python 3.4

<?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> 

Basierend auf der Actioncommand i entweder einen Block immer bin, ist das Problem nicht die Untergeordnete Tag (ValidationStep) und seine untergeordneten Tags alle bekommen konnte . Kann jemand helfen?

Mein Code:

for testSetup4 in root.findall(".TestCase/TestSetup/Action"): 
    if testSetup4.find('ActionCommand').text == "gfdg": 
     for c1 in testSetup4: 
      t2.append(c1.tag) 
      v2.append(c1.text) 

     for k,v in zip(t2, v2): 
      test_case[k] = v 

Ich bin nicht in der Lage ValidationStep (sub Eltern) und seine entsprechenden Tags zu erhalten.

Antwort

0

Fügen Sie einfach eine weitere Schleife hinzu, um den Knoten <ValidationStep> und seine untergeordneten Elemente zu durchlaufen. Auch Sie nicht brauchen die beiden anderen Listen, wie Sie ein Wörterbuch während der Parsing-Schleife aktualisieren:

import xml.etree.ElementTree as et 

dom = et.parse('Input.xml') 
root = dom.getroot() 

test_case = {} 
for testSetup4 in root.findall(".TestCase/TestSetup/Action"): 
    if testSetup4.find('ActionCommand').text == "gfdg": 
     for c1 in testSetup4: 
      test_case[c1.tag]= c1.text 
     for vd in testSetup4.findall("./ValidationStep/*"): 
      test_case[vd.tag]= vd.text 

Alternativ verwenden Sie den doppelten Schrägstrich Operator für alle Kinder mit Enkel von <Action> Element zu suchen:

for testSetup4 in root.findall(".TestCase/TestSetup/Action"): 
    if testSetup4.find('ActionCommand').text == "gfdg": 
     for c1 in testSetup4.findall(".//*"): 
      test_case[c1.tag]= c1.text