Python 寫入XML 檔案

2021-06-29 00:50:31 字數 967 閱讀 2373

需要將test case轉為xml檔案方式用於讀取。

**中test_suit.txt 是用來儲存原來的case檔案的,故做了一點string的處理,用split() 函式摳出自己需要的字段

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)

root.toxml()

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

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

f.close

結構比較簡單,就只有test suit 和test case 兩個層次。

C Doc寫入 XML檔案

html是xml的先驅,xml延續了html的簡單性的優點。xml不是用來替代html的,xml和html為不同的目的而設計 xml被設計用來描述資料,其焦點是資料的內容。html被設計用來顯示資料,其焦點是資料的外觀。html旨在顯示資訊,而xml旨在描述資訊html是xml的先驅,xml延續了h...

XML檔案寫入與讀取

把這個東西做成通用方法 以後直接傳引數就可以了 很方便 以下為寫入 建立乙個新的guest節點並將它新增到根節點下 xmlelement parentnode xdoc.createelement guest xdoc.documentelement.prependchild parentnode ...

將XML檔案寫入文件

public static void main string args throws exception 建立乙個空的document物件 document doc documenthelper.createdocument 建立乙個根元素 element stus doc.addelement s...