0

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

2
  • Your xml is invalid. It's missing the closing </TestSetup>. Commented Jul 22, 2016 at 14:21
  • Sorry about that ! Copy paste issue Commented Jul 22, 2016 at 14:41

3 Answers 3

1

You are not using lxml, you are currently using xml.etree.ElementTree from the Python standard library.

If you were to actually use lxml, assuming you have it installed, change your import to:

import lxml.etree as ET

Then, you can check the ActionCommand value right inside the XPath expression:

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
Sign up to request clarification or add additional context in comments.

Comments

0

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)

1 Comment

Thanks ! Will check it :)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.