测试交流学习QQ:476879428

QTP自动化测试--VBS之XML文件操作

上一篇 / 下一篇  2014-03-28 18:32:13 / 个人分类:自动化测试

1.

Dim xmlDoc, rootEl, child1, child2, p

'创建XML文档

Set xmlDoc = CreateObject("MSXML2.DOMDocument")

'创建根元素并将之加入文档

Set rootE1=xmlDoc.createElement("BookStore")

xmlDoc.appendChild rootE1

'创建并加入子元素

Set bookchild1=xmlDoc.createElement("book")

Set ISDNAttribute=xmlDoc.createAttribute("ISDN")

ISDNAttribute.text="789456123"

bookchild1.setAttributeNode ISDNAttribute

Set bookchild1_title=xmlDoc.createElement("title")

bookchild1_title.text="C#"

bookchild1.appendChild bookchild1_title

Set bookchild1_author=xmlDoc.createElement("author")

bookchild1_author.text="BXH"

bookchild1.appendChild bookchild1_author

Set bookchild1_price=xmlDoc.createElement("Price")

bookchild1_price.text="29.3"

bookchild1.appendChild bookchild1_price

rootE1.appendChild bookchild1

'创建并加入子元素

Set bookchild2=xmlDoc.createElement("book")

Set ISDNAttribute=xmlDoc.createAttribute("ISDN")

ISDNAttribute.text="789457898"

bookchild2.setAttributeNode ISDNAttribute

Set bookchild2_title=xmlDoc.createElement("title")

bookchild2_title.text="F#"

bookchild2.appendChild bookchild2_title

Set bookchild2_author=xmlDoc.createElement("author")

bookchild2_author.text="mary"

bookchild2.appendChild bookchild2_author

Set bookchild2_price=xmlDoc.createElement("Price")

bookchild2_price.text="78"

bookchild2.appendChild bookchild2_price

rootE1.appendChild bookchild2

'创建XML processing instruction

'并把它加到根元素之前

Set p=xmlDoc.createProcessingInstruction("xml","version='1.0'")

xmlDoc.insertBefore p,xmlDoc.childNodes(0)

'把文件保存到c目录

xmlDoc.Save "c:\test.xml"

运行完毕之后,打开"c:\test.xml",显示如下: 

    <?xml version="1.0" ?>

-    <BookStore>

-        <book ISDN="789456123">

              <title>C#</title>

<             <author>BXH</author>

<            <Price>29.3</Price>

        </book>

-       <book ISDN="789457898">

<            <title>F#</title>

              <author>mary</author>

<           <Price>78</Price>

         </book>

</BookStore>

 

 

2.演示一个例子,用以读取XML指定节点的节点内容值。

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

 


TAG:

 

评分:0

我来说两句

Open Toolbar