简体   繁体   English

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

[英]Parsing a complex xml in python lxml parser

I have the follwoing 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>

The issue is I could not get the subparent tag (validationStep) and all its child values. 问题是我无法获得父级标签(validationStep)及其所有子值。 can anyone help. 谁能帮忙。

My code : 我的代码:

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

Kindly help me in this issue, I am new to lxml parser. 请帮助我解决此问题,我是lxml解析器的新手。

You are not using lxml , you are currently using xml.etree.ElementTree from the Python standard library. 您未使用lxml ,当前正在使用Python标准库中的xml.etree.ElementTree

If you were to actually use lxml , assuming you have it installed, change your import to: 如果要实际使用lxml ,并假设已安装,则将导入更改为:

import lxml.etree as ET

Then, you can check the ActionCommand value right inside the XPath expression: 然后,您可以在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

If I understand you correctly, you need something like this: 如果我对您的理解正确,则需要这样的内容:

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)

This is done . 这个完成了 。 Here is my code : 这是我的代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM