您的位置:首页 > 编程语言 > Python开发

Python 写入XML 文件

2015-03-09 20:35 260 查看
转载于博客地址:

http://blog.csdn.net/liuyuehui110/article/details/7287897

需要将test case转为XML文件方式用于读取。

代码中test_suit.txt 是用来存储原来的case文件的,故做了一点string的处理,用split() 函数抠出自己需要的字段

<pre name="code" class="python">import xml.dom.minidom

impl = xml.dom.minidom.getDOMImplementation()

dom = impl.createDocument(None, 'catalog', None)

root = dom.documentElement

file = open('/root/test_suit.txt', 'r')

info = file.readlines()

testsuit = None

for i in range(0, len(info)):
if ( testsuit != info[i].split(' ')[2].split('\'')[1].split('\'')[0]):
testsuit = info[i].split(' ')[2].split('\'')[1].split('\'')[0]
item = dom.createElement(testsuit)
testcase = info[i].split(' ')[4].split('\'')[1].split('\'')[0]
casenode = dom.createElement('case')
text = dom.createTextNode(testcase)
casenode.appendChild(text)
item.appendChild(casenode)
root.appendChild(item)
root.toxml()

f = open('/root/testsuit.xml', 'w')

dom.writexml(f, '', '', '')
f.close



结构比较简单,就只有test suit 和test case 两个层次。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python xml dom