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, 比如1这个节点,它的childNodes.item(0).nodeName竟然是“#text”,但是根据例子来看TestResult已经没有子节点了阿 回答: 因为在xml有一个特殊的“子节点”——文本节点。比如 100 这个节点TestResult下并不是没有子节点,而是有一个文本节点,这个节点的nodeName就是“#text”,而nodeValue是100.如果是 这种节点的话,那么用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学习总结