在python lxml解析器中解析复杂的xml

时间:2016-07-22 14:14:20

标签: python xml

我有下面的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>
    </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解析器的新手。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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)

答案 2 :(得分: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