用VBS对XML 的读取

上一篇 / 下一篇  2008-08-22 10:53:27

用VBS对XML 的读取

VBS脚本:

Dim strXML
GetXml "c:\task01.xml","TestNumberOne0"  '该行仅为实例,前面为xml文件路径,后面为查找的tag名称
MsgBox strXML

Function GetXml (ByVal strXmlFilePath,ByVal xmlNodeName)
        Dim xmlDoc,xmlRoot
        
        Set xmlDoc = CreateObject("Microsoft.XMLDOM")
        xmlDoc.async = False
        xmlDoc.load strXmlFilePath      
        If xmlDoc.parseError.errorCode <> 0 Then
                MsgBox "XML文件格式不对,原因是:" & Chr(13) &  xmlDoc.parseError.reason
                Exit Function               
        End If
        Set xmlRoot = xmlDoc.documentElement        
        xmlRecursion xmlRoot,xmlNodeName        
        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

XML文件内容:c:\task01.xml

<?xml version="1.0" encoding="GB2312"?>

<ROOT>
        <TestCase>
                <TestNumberOne0>1</TestNumberOne0>
                <TestNumberTwo>2</TestNumberTwo>
                <TestNumberThree>+</TestNumberThree>
                <TestResult>3</TestResult>
        </TestCase>
        <TestCase>
                <TestNumberOne1>3</TestNumberOne1>
                <TestNumberTwo>2</TestNumberTwo>
                <TestNumberThree>-</TestNumberThree>
                <TestResult>1</TestResult>
        </TestCase>
        <TestCase>
                <TestNumberOne2>3</TestNumberOne2>
                <TestNumberTwo>7</TestNumberTwo>
                <TestNumberThree>*</TestNumberThree>
                <TestResult>21</TestResult>
        </TestCase>
        <TestCase>
                <TestNumberOne3>2</TestNumberOne3>
                <TestNumberTwo>5</TestNumberTwo>
                <TestNumberThree>/</TestNumberThree>
                <TestResult>0.4</TestResult>
        </TestCase>        
</ROOT>
 

TAG:

 

评分:0

我来说两句

我的栏目

日历

« 2024-05-03  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 6007
  • 日志数: 11
  • 建立时间: 2007-11-17
  • 更新时间: 2008-08-22

RSS订阅

Open Toolbar