QTP中读写xml可以使用其自带的组件,下面是简单的使用脚本。
'''
'''The class use to write data for test result report
''''===========Demo to use this class=====================
'Dim xmlobj
'Set xmlobj = new XML
'xmlobj.init
'xmlobj.add_case 1
'xmlobj.add_content 1
'xmlobj.add_casename "casename"
'xmlobj.add_pagename "pagename"
'xmlobj.save "D:\test.xml"
''''======================================================
Class XML
Dim oXML, root, currentcase, currentheader, currentcontent
Function init(file_name)
'创建xml保留对象组件
set oXML = XMLUtil.CreateXML
'新建一个document
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(file_name) Then
oXML.LoadFile file_name ''''文件存在则读取文件
else
oXML.CreateDocument "prodtest" ''''文件不存在则创建根节点
End If
'获取root根元素
set root = oXML.GetRootElement ''''获取根元素
End Function
Function add_case()
'添加一个空的子节点case
root.AddChildElementByName "case",""
int_case_index = root.ChildElements.Count
Set currentcase = root.ChildElements.Item(int_case_index)
add_header()
End Function
Function add_header()
currentcase.AddChildElementByName "header",""
Set currentheader = currentcase.ChildElements.ItemByName("header")
End Function
Function add_content()
currentcase.AddChildElementByName "content",""
int_content_index = currentcase.ChildElements.AllItemsByName("content").Count
Set currentcontent = currentcase.ChildElements.ItemByName("content", int_content_index)
End Function
Function add_casename(casename)
currentheader.AddChildElementByName "testcase", casename '''''添加带指定内容的子节点
End Function
Function add_starttime(starttime)
currentheader.AddChildElementByName "starttime", starttime
End Function
Function add_endtime(endtime)
currentheader.AddChildElementByName "endtime", endtime
End Function
Function add_passcount(passcount)
currentheader.AddChildElementByName "passcount", passcount
End Function
Function add_failcount(failcount)
currentheader.AddChildElementByName "failcount", failcount
End Function
Function add_donecount(donecount)
currentheader.AddChildElementByName "donecount", donecount
End Function
Function add_pagename(page)
currentcontent.AddChildElementByName "page", page
End Function
Function add_url(url)
currentcontent.AddChildElementByName "url", url
End Function
Function add_element(element)
currentcontent.AddChildElementByName "element", element
End Function
Function add_function(function_name)
currentcontent.AddChildElementByName "function", function_name
End Function
Function add_reason(reason)
currentcontent.AddChildElementByName "reason", reason
End Function
Function add_detail(detail)
currentcontent.AddChildElementByName "detail", detail
End Function
Function add_screen(screen_file)
currentcontent.AddChildElementByName "screenshot", "img\"+screen_file
End Function
Function add_index(int_index)
currentcontent.AddChildElementByName "index", int_index
End Function
Function save(file_name)
'保存文件
oXML.SaveFile file_name ''''保存文件
'释放对象
Set root = nothing
Set oXML = nothing
End Function
End Class
当然你也可以使用vbs支持的xml组件来读写。比如:
Microsoft.XMLDOM。更多关于XMLUtil对象的使用方法请查阅QTP自带帮助文档(Utility.chm)