2016-07-21 8 views
0

:私は1つのブロックのいずれかを取得していますアクションコマンドに基づいて解析複雑なXMLのPython私は次のXMLを持っている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> 

、問題のサブ親タグ(ValidationStep)とそのすべての子タグを取得できませんでしたです。誰も助けることができますか?

マイコード:

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 

私はValidationStep(サブ親)とそれに対応するタグを取得することはできませんよ。

答えて

0

<ValidationStep>ノードとその子を通して反復するループを追加するだけです。あなたが解析ループの中に辞書を更新することができますよう。また、次の2つの他のリストを必要としません。また

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 

<Action>要素の孫を含むすべての子供たちのために検索するためにダブルスラッシュ演算子を使用します。

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 
関連する問題