Merging elements inside a xml.etree.ElementTree

Posted by theAlse on Stack Overflow See other posts from Stack Overflow or by theAlse
Published on 2014-06-05T14:10:45Z Indexed on 2014/06/08 21:25 UTC
Read the original article Hit count: 320

Filed under:
|
|

I have a huge test data like the one provided below (and yes I have no control over this data). Each line is actually 6 parts and I need to generate an XML based on this data.

Nav;Basic;Dest;Smoke;No;Yes;
Nav;Dest;Recent;Regg;No;Yes;
Nav;Dest;Favourites;Regg;No;Yes;
...
Nav;Dest using on board;By POI;Smoke;No;Yes;
Nav;Dest using on board;Other;Regg;No;Yes;

The first 3 elements on each line denotes "test suites"-XML element and the last 3 element should create a "test case"-XML element.

I have successfully converted it into a XML using the following code:

# testsuite (root)
testsuite = ET.Element('testsuite')
testsuite.set("name", "Tests")

def _create_testcase_tag(elem):
    global testsuite

    level1, level2, level3, elem4, elem5, elem6 = elem

    # -- testsuite (level1)
    testsuite_level1 = ET.SubElement(testsuite, "testsuite")
    testsuite_level1.set("name", level1)

    # -- testsuite (level2)
    testsuite_level2 = ET.SubElement(testsuite_level1, "testsuite")
    testsuite_level2.set("name", level2)

    # -- testsuite (level3)
    testsuite_level2 = ET.SubElement(testsuite_level2, "testsuite")
    testsuite_level2.set("name", level3)

    # -- testcase
    testcase = ET.SubElement(testsuite_level2, "testcase")
    testcase.set("name", "TBD")
    summary = ET.SubElement(testcase, "summary")
    summary.text = "Test Type= %s, Automated= %s, Available=%s" %(elem4, elem5, elem6)

with open(input_file) as in_file:
    for line_number, a_line in enumerate(in_file):
        try:
            parameters = a_line.split(';')
            if len(parameters) >= 6:
                level1 = parameters[0].strip()
                level2 = parameters[1].strip()
                level3 = parameters[2].strip()
                elem4 = parameters[3].strip()
                elem5 = parameters[4].strip()
                elem6 = parameters[5].strip()

                lines_as_list.append((level1, level2, level3, elem4, elem5, elem6))
        except ValueError:
            pass

lines_as_list.sort()
for elem in lines_as_list:
    _create_testcase_tag(elem)

output_xml = ET.ElementTree(testsuite)
ET.ElementTree.write(output_xml, output_file, xml_declaration=True, encoding="UTF-8")

The above code generates an XML like this:

<testsuite name="Tests">
    <testsuite name="Nav">
        <testsuite name="Basic navigation">
            <testsuite name="Set destination">
                <testcase name="TBD">
                    <summary>Test Type= Smoke test Automated= No, Available=Yes</summary>
                </testcase>
            </testsuite>
        </testsuite>
    </testsuite>

    <testsuite name="Nav">
        <testsuite name="Set destination">
            <testsuite name="Recent">
                <testcase name="TBD">
                    <summary>
                    Test Type= Reggression test Automated= No, Available=Yes
                    </summary>
                </testcase>
            </testsuite>
        </testsuite>
    </testsuite>
</testsuite>
...

This is all correct, but as you can see I have created a whole tree for each line and that is not what I need. I need to combine e.g. all testsuite with the same name into one testsuite and also perform that recursively. So the XML looks like this instead:

<testsuite name="Tests">
    <testsuite name="Nav">
        <testsuite name="Basic navigation">
            <testsuite name="Set destination">
                <testcase name="TBD">
                    <summary>Test Type= Smoke test Automated= No, Available=Yes</summary>
                </testcase>
            </testsuite>
            <testsuite name="Recent">
                <testcase name="TBD">
                    <summary>
                    Test Type= Reggression test Automated= No, Available=Yes
                    </summary>
                </testcase>
            </testsuite>
        </testsuite>
    </testsuite>
</testsuite>

I hope you can understand what I mean, but level1, level2 and level3 should be unique with testcases inside.

How should I do this? Please do not suggest the use of any external libraries! I can not install new libraries in customer site. xml.etree.ElementTree is all I have.

Thanks

© Stack Overflow or respective owner

Related posts about python

Related posts about Xml