1 回答
TA贡献1963条经验 获得超6个赞
以下:
import xml.etree.ElementTree as ET
xml = '''<testsuite name="Setup">
<testcase classname="Configuration" name="xxx">
<data>abc_qwe</data>
</testcase>
<testcase classname="Configuration" name="yyy">
<data>xyzzzz</data>
</testcase>
</testsuite>'''
root = ET.fromstring(xml)
test_cases = root.findall('.//testcase')
for test_case in test_cases:
test_case.find('./data').text = test_case.attrib['name']
ET.dump(root)
输出
<testsuite name="Setup">
<testcase classname="Configuration" name="xxx">
<data>xxx</data>
</testcase>
<testcase classname="Configuration" name="yyy">
<data>yyy</data>
</testcase>
</testsuite>
另一种方式(用数据文本设置name属性的值)
import xml.etree.ElementTree as ET
xml = '''<testsuite name="Setup">
<testcase classname="Configuration" name="xxx">
<data>data_1</data>
</testcase>
<testcase classname="Configuration" name="yyy">
<data>data_2</data>
</testcase>
</testsuite>'''
root = ET.fromstring(xml)
test_cases = root.findall('.//testcase')
for test_case in test_cases:
test_case.attrib['name'] = test_case.find('./data').text
ET.dump(root)
添加回答
举报