QTP操作xml文件方法
生活随笔
收集整理的這篇文章主要介紹了
QTP操作xml文件方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
搜索1:
目前,企業中對XML的應用越來越廣泛,作為自動化測試的測試工程師,也應該掌握XML的讀寫操作。
以下我使用XML DOM技術演示一個例子,用以讀取XML指定節點的節點內容值。
讀取函數原型 GetXml strXmlPath,nodeName
這個函數的第一個參數表示xml文件所在路徑,第二個參數表示希望獲取到的xml節點名,請結合下列例子看
首先,新建一個vbs文件(取個名字叫readXml.vbs),輸入代碼:
Dim strXML
GetXml "c:search.xml","TestResult" '這個函數的第一個參數表示xml文件所在路徑,第二個參數表示希望獲取到的xml節點名,請結合下列例子看
MsgBox strXML
Function GetXml (ByVal strXmlFilePath,ByVal xmlNodeName)
Dim xmlDoc,xmlRoot
Set xmlDoc = CreateObject("Microsoft.XMLDOM") '創建XML DOM對象
xmlDoc.async = False '控制加載模式為同步模式(xml樹加載完畢后再執行后續代碼)
xmlDoc.load strXmlFilePath '載入xml文件
If xmlDoc.parseError.errorCode <> 0 Then
MsgBox "XML文件格式不對,原因是:" & Chr(13) & xmlDoc.parseError.reason
Exit Function
End If
Set xmlRoot = xmlDoc.documentElement
xmlRecursion xmlRoot,xmlNodeName '調用xml遞歸函數傳入指定的根和節點名
GetXml = True 'xmlRecursion (xmlRoot)
End Function
Function xmlRecursion(byval xmlNode,byval strNodeName)
If xmlNode.nodeName = strNodeName And xmlNode.hasChildNodes Then
If xmlNode.childNodes.item(0).nodeName = "#text" Then
strXML = strXML & xmlNode.nodeName & ":" & xmlNode.childNodes.item(0).nodeValue & Chr(13)
End If
End If
If xmlNode.hasChildNodes Then
For Each childNodeItem In xmlNode.ChildNodes
If childNodeItem.hasChildNodes Then
xmlRecursion childNodeItem,strNodeName
End If
Next
End If
End Function
問題:
haschildnodes()這個方法好奇怪,明明已經沒有子節點了,卻仍然返回true,
比如<TestResult>1</TestResult>這個節點,它的childNodes.item(0).nodeName竟然是“#text”,但是根據例子來看TestResult已經沒有子節點了阿
回答:
因為在xml有一個特殊的“子節點”——文本節點。比如 <TestResult>100</TestResult>
這個節點TestResult下并不是沒有子節點,而是有一個文本節點,這個節點的nodeName就是“#text”,而nodeValue是100.如果是 <TestResult/> 這種節點的話,那么用hasChildNodes則返回False
遍歷xml的代碼:
Option Explicit
Dim xmlDoc,myErr,strXML
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.load "c:calc1.xml"
If xmlDoc.parseError.errorCode <> 0 Then
Set myErr = xmlDoc.parseError
MsgBox("XML Loads Failed. " & myErr.reason)
Else
Set rootNode = xmlDoc.documentElement
Call rTravel(rootNode)
MsgBox strXML
End If
Sub rTravel (rNode)
Dim blnTwo,intTestCase,
blnTwo = False
iLen = rNode.childNodes.length
If iLen > 0 Then
For i = 0 To rNode.childNodes.length -1
Set child = rNode.childNodes.item(i)
Call rTravel(child)
childtext = child.nodeValue
strXML = strXML & childtext & chr(13)
Next
Else
Exit Sub
End If
End Sub
方法二:
Option Explicit
Dim xmlDoc,myErr,strXML
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.load "c:calc1.xml"
If xmlDoc.parseError.errorCode <> 0 Then
Set myErr = xmlDoc.parseError
MsgBox("XML Loads Failed. " & myErr.reason)
Else
Set rootNode = xmlDoc.documentElement
Call rTravel(rootNode)
MsgBox strXML
End If
Sub rTravel (rNode)
Dim blnTwo,intTestCase,
blnTwo = False
iLen = rNode.childNodes.length
If iLen > 0 Then
For i = 0 To rNode.childNodes.length -1
Set child = rNode.childNodes.item(i)
Call rTravel(child)
childtext = child.nodeValue
strXML = strXML & childtext & chr(13)
Next
Else
Exit Sub
End If
End Sub
搜索2:
Dim filepath,xmlDoc,myErr,strXML,rootNode
filepath="c:\12.xml"
Set xmlDoc=CreateObject("Microsoft.XMLDOM")'創建一個xml對象
xmlDoc.async=False
xmlDoc.load filepath '加載xml文件
If xmlDoc.parseError.errorCode<>0 Then'返回錯誤信息
Set myErr=xmlDoc.parseError
MsgBox("XML Loads Faild."&myErr.reason)
Else
Set rootNode=xmlDoc.documentElement'設置根節點
Call rTravel(rootNode)'遍歷節點
msgBox strXML
End If
Sub rTravel(rNode)
Dim blnTwo,intTestCase,iLen,i,child
blnTwo=False
iLen=rNode.childNodes.length'返回節點的子節點數目
If iLen>0 Then
For i=0 to rNode.childNodes.length-1
Set child=rNode.childNodes.item(i)'獲取當前節點對象
Call rTravel(child)
childtext=child.nodeValue'獲取節點的值
strXML=strXML & childtext & chr(13)'chr(13)回車符
Next
else
Exit Sub
End If
End Sub
另附搜索到的qtp_xml學習總結
目前,企業中對XML的應用越來越廣泛,作為自動化測試的測試工程師,也應該掌握XML的讀寫操作。
以下我使用XML DOM技術演示一個例子,用以讀取XML指定節點的節點內容值。
讀取函數原型 GetXml strXmlPath,nodeName
這個函數的第一個參數表示xml文件所在路徑,第二個參數表示希望獲取到的xml節點名,請結合下列例子看
首先,新建一個vbs文件(取個名字叫readXml.vbs),輸入代碼:
Dim strXML
GetXml "c:search.xml","TestResult" '這個函數的第一個參數表示xml文件所在路徑,第二個參數表示希望獲取到的xml節點名,請結合下列例子看
MsgBox strXML
Function GetXml (ByVal strXmlFilePath,ByVal xmlNodeName)
Dim xmlDoc,xmlRoot
Set xmlDoc = CreateObject("Microsoft.XMLDOM") '創建XML DOM對象
xmlDoc.async = False '控制加載模式為同步模式(xml樹加載完畢后再執行后續代碼)
xmlDoc.load strXmlFilePath '載入xml文件
If xmlDoc.parseError.errorCode <> 0 Then
MsgBox "XML文件格式不對,原因是:" & Chr(13) & xmlDoc.parseError.reason
Exit Function
End If
Set xmlRoot = xmlDoc.documentElement
xmlRecursion xmlRoot,xmlNodeName '調用xml遞歸函數傳入指定的根和節點名
GetXml = True 'xmlRecursion (xmlRoot)
End Function
Function xmlRecursion(byval xmlNode,byval strNodeName)
If xmlNode.nodeName = strNodeName And xmlNode.hasChildNodes Then
If xmlNode.childNodes.item(0).nodeName = "#text" Then
strXML = strXML & xmlNode.nodeName & ":" & xmlNode.childNodes.item(0).nodeValue & Chr(13)
End If
End If
If xmlNode.hasChildNodes Then
For Each childNodeItem In xmlNode.ChildNodes
If childNodeItem.hasChildNodes Then
xmlRecursion childNodeItem,strNodeName
End If
Next
End If
End Function
問題:
haschildnodes()這個方法好奇怪,明明已經沒有子節點了,卻仍然返回true,
比如<TestResult>1</TestResult>這個節點,它的childNodes.item(0).nodeName竟然是“#text”,但是根據例子來看TestResult已經沒有子節點了阿
回答:
因為在xml有一個特殊的“子節點”——文本節點。比如 <TestResult>100</TestResult>
這個節點TestResult下并不是沒有子節點,而是有一個文本節點,這個節點的nodeName就是“#text”,而nodeValue是100.如果是 <TestResult/> 這種節點的話,那么用hasChildNodes則返回False
遍歷xml的代碼:
Option Explicit
Dim xmlDoc,myErr,strXML
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.load "c:calc1.xml"
If xmlDoc.parseError.errorCode <> 0 Then
Set myErr = xmlDoc.parseError
MsgBox("XML Loads Failed. " & myErr.reason)
Else
Set rootNode = xmlDoc.documentElement
Call rTravel(rootNode)
MsgBox strXML
End If
Sub rTravel (rNode)
Dim blnTwo,intTestCase,
blnTwo = False
iLen = rNode.childNodes.length
If iLen > 0 Then
For i = 0 To rNode.childNodes.length -1
Set child = rNode.childNodes.item(i)
Call rTravel(child)
childtext = child.nodeValue
strXML = strXML & childtext & chr(13)
Next
Else
Exit Sub
End If
End Sub
方法二:
Option Explicit
Dim xmlDoc,myErr,strXML
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.load "c:calc1.xml"
If xmlDoc.parseError.errorCode <> 0 Then
Set myErr = xmlDoc.parseError
MsgBox("XML Loads Failed. " & myErr.reason)
Else
Set rootNode = xmlDoc.documentElement
Call rTravel(rootNode)
MsgBox strXML
End If
Sub rTravel (rNode)
Dim blnTwo,intTestCase,
blnTwo = False
iLen = rNode.childNodes.length
If iLen > 0 Then
For i = 0 To rNode.childNodes.length -1
Set child = rNode.childNodes.item(i)
Call rTravel(child)
childtext = child.nodeValue
strXML = strXML & childtext & chr(13)
Next
Else
Exit Sub
End If
End Sub
搜索2:
Dim filepath,xmlDoc,myErr,strXML,rootNode
filepath="c:\12.xml"
Set xmlDoc=CreateObject("Microsoft.XMLDOM")'創建一個xml對象
xmlDoc.async=False
xmlDoc.load filepath '加載xml文件
If xmlDoc.parseError.errorCode<>0 Then'返回錯誤信息
Set myErr=xmlDoc.parseError
MsgBox("XML Loads Faild."&myErr.reason)
Else
Set rootNode=xmlDoc.documentElement'設置根節點
Call rTravel(rootNode)'遍歷節點
msgBox strXML
End If
Sub rTravel(rNode)
Dim blnTwo,intTestCase,iLen,i,child
blnTwo=False
iLen=rNode.childNodes.length'返回節點的子節點數目
If iLen>0 Then
For i=0 to rNode.childNodes.length-1
Set child=rNode.childNodes.item(i)'獲取當前節點對象
Call rTravel(child)
childtext=child.nodeValue'獲取節點的值
strXML=strXML & childtext & chr(13)'chr(13)回車符
Next
else
Exit Sub
End If
End Sub
另附搜索到的qtp_xml學習總結
總結
以上是生活随笔為你收集整理的QTP操作xml文件方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 接口继承中一个常见问题的思考
- 下一篇: QTP中对用户自定义环境变量的XML操作