2016-07-22 4 views
0

で複雑なXMLを解析、私はfollwoing XMLを持っているPythonのlxmlのパーサ

<?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> 
    </TestCase> 
</Suite> 

問題は、私はsubparentタグ(validationStep)とそのすべての子の値を取得できませんでしたです。誰でも助けることができます。

マイコード:

import xml.etree.ElementTree as ET 
import collections 
t2 =[] 
v2 =[] 
test_case = collections.OrderedDict() 
tree = ET.parse('Action123.xml') 
root = tree.getroot() 

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 

親切にこの問題で私を助けて、私はパーサをlxmlのために新しいです。

+0

あなたのXMLは無効です。終了の ''がありません。 –

+0

ごめんなさい!ペーストのコピーをコピーしてください – user2829222

答えて

1

lxmlを使用していません。現在、Python標準ライブラリのxml.etree.ElementTreeを使用しています。

あなたが実際にlxmlを使用した場合、あなたはそれがインストールされていると仮定すると、へのインポートを変更します。次に

import lxml.etree as ET 

、あなたは右のXPath式の内側ActionCommand値を確認することができます。

for testSetup4 in root.xpath(".//TestCase/TestSetup/Action[ActionCommand = 'gfdg']"): 
    for c1 in testSetup4: 
     t2.append(c1.tag) 
     v2.append(c1.text) 

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

ありがとうございます。それを試してみる:) – user2829222

0

わかりましたら、このようなものが必要です:

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

ありがとう!それをチェックする:) – user2829222

0

これは完了です。ここに私のコードです:

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

     for k,v in zip(t1, v1): 
      test_case[k] = v 

     valid = testSetup4.find('ValidationStep') 
     for c2 in valid: 
      t2.append(c2.tag) 
      v2.append(c2.text) 

     for k,v in zip(t2, v2): 
      test_case[k] = v 
関連する問題