合并xml.etree.ElementTree中的元素

时间:2014-06-05 14:10:45

标签: python xml elementtree

我有一个巨大的测试数据,如下面提供的那样(是的,我无法控制这些数据)。每行实际上是6个部分,我需要根据这些数据生成XML。

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;

每行的前3个元素表示“test suites"-XML element”,最后3个元素应创建"test case"-XML element

我已使用以下代码将其成功转换为XML:

# 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")

以上代码生成如下XML:

<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>
...

这一切都是正确的,但正如你所看到的,我为每一行创建了一棵完整的树,这不是我需要的。我需要结合例如具有相同名称的所有testsuite到一个testsuite中,并且还递归执行。 所以XML看起来像这样:

<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>

我希望你能理解我的意思,但是level1,level2和level3对于里面的测试用例应该是唯一的。

我该怎么做?请不要建议使用任何外部库!我无法在客户站点中安装新库。我只有xml.etree.ElementTree

由于

1 个答案:

答案 0 :(得分:1)

在前三个字段中对数据进行排序,然后使用itertools.groupby()在添加到xml之前按照您想要的方式组织数据。 这假定您的示例中显示的嵌套级别

from operator import itemgetter
from itertools import groupby

names = itemgetter(slice(3))
test_items = itemgetter(slice(3,None))
level1 = itemgetter(0)
level2 = itemgetter(1)
level3 = itemgetter(2)


s = """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;Recent;Regg;Yes;Yes;
Nav;Dest using on board;Other;Regg;No;Yes;
Nav;Basic;Dest;Smoke;Yes;Yes;
Nav;Basic;Dest;Smoke;Yes;No;
"""
data = list()
for line in s.splitlines():
    data.append(line.rstrip(';').split(';'))
data.sort(key = names)
##for thing in data:
##    print thing

testsuite = ET.Element('testsuite')
testsuite.set("name", "Tests")
for key1, group1 in groupby(data, level1):
    print '***', key1
    testsuite_level1 = ET.SubElement(testsuite, "testsuite")
    testsuite_level1.set("name", key1)
    for key2, group2 in groupby(group1, level2):
        print '******', key2
        testsuite_level2 = ET.SubElement(testsuite_level1, "testsuite")
        testsuite_level2.set("name", key2)
        for key3, group3 in groupby(group2, level3):
            print '*********', key3
            testsuite_level3 = ET.SubElement(testsuite_level2, "testsuite")
            testsuite_level3.set("name", key3)
            testcase = ET.SubElement(testsuite_level3, "testcase")
            testcase.set("name", "TBD")
            for element in group3:
                print '         ', test_items(element)
                summary = ET.SubElement(testcase, "summary")
                summary.text = "Test Type= {}, Automated= {}, Available={}".format(*test_items(element))

>>>
*** Nav
****** Basic
********* Dest
          ['Smoke', 'No', 'Yes']
          ['Smoke', 'Yes', 'Yes']
          ['Smoke', 'Yes', 'No']
****** Dest
********* Favourites
          ['Regg', 'No', 'Yes']
********* Recent
          ['Regg', 'No', 'Yes']
          ['Regg', 'Yes', 'Yes']
****** Dest using on board
********* By POI
          ['Smoke', 'No', 'Yes']
********* Other
          ['Regg', 'No', 'Yes']
>>>

在回应评论proper XML solution时,我提出了这个问题 - 也许这就是你的想法。我必须制作唯一标记,而不是每个标记都为testsuite。这有利于在运行中创建树

def _create_testcase_tag(testsuite, elem):
    """Add elem to testsuite.

    testsuite --> xml.etree.ElementTree
    elem --> list

    return xml.etree.ElementTree
    """

    level1, level2, level3, elem4, elem5, elem6 = elem
    level1 = level1.replace(' ','_')
    level2 = level2.replace(' ','_')
    level3 = level3.replace(' ','_')

    # -- testsuite (level1)
    testsuite_level1 = testsuite.find(level1)
    if not testsuite_level1:
        testsuite_level1 = ET.SubElement(testsuite, level1)

    # -- testsuite (level2)
    testsuite_level2 = testsuite_level1.find(level2)
    if not testsuite_level2:
        testsuite_level2 = ET.SubElement(testsuite_level1, level2)

    # -- testsuite (level3)
    testsuite_level3 = testsuite_level2.find(level3)
    if not testsuite_level3:
        testsuite_level3 = ET.SubElement(testsuite_level2, level3)

    # -- testcase
    testcase = ET.SubElement(testsuite_level3, "testcase")
    testcase.set("name", "TBD")
    summary = ET.SubElement(testcase, "summary")
    summary.text = "Test Type= {}, Automated= {}, Available={}".format(elem4, elem5, elem6)

    return testsuite

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

with open(input_file) as in_file:
    for line in in_file:
        line = line.strip().rstrip(';').split(';')
        testsuite = _create_testcase_tag(testsuite, line)