一个离开.NET的程序员

ryhan

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
-------------------------------------------------------------------------------------------------------
Function GetXml (strXmlFilePath,nodestring)
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    
Nodes =  Split(nodestring, "\") 
GetXml = xmlRecursion(xmlRoot,Nodes,1,true)  
set xmlDoc = Nothing
End Function



Function xmlRecursion(byval xmlNode,NodeNames,NodeIndex,isContinue)
If xmlNode.hasChildNodes Then
If  xmlNode.childNodes.item(0).nodeName = "#text" Then
xmlRecursion= xmlNode.childNodes.item(0).nodeValue
isContinue = false
End If              
End If                      
If xmlNode.hasChildNodes Then
For Each childNodeItem In xmlNode.ChildNodes
If  isContinue Then
If childNodeItem.hasChildNodes and childNodeItem.nodeName = NodeNames(NodeIndex) Then
xmlRecursion = xmlRecursion(childNodeItem,NodeNames,NodeIndex+1,isContinue)     
End If           
End If
Next
End If      
End Function

---------------------------------------------------------------------------------------------------



调用的时候直接写 GetXml(XML文件路径,节点路径)
如:msgbox GetXml("c:\test2.xml","根节点\子节点")

如果xml文件的内容是下面的内容的话,  msgbox GetXml("文件路径","ROOT\TestCase0\TestResult")  就会显示出 “3”,此函数不适取节点的属性。以后再完善吧,呵呵

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

<ROOT>
<TestCase0>
<TestNumberOne>1</TestNumberOne>
<TestNumberTwo>2</TestNumberTwo>
<TestNumberThree>+</TestNumberThree>
<TestResult>3</TestResult>
</TestCase0>
<TestCase1>
<TestNumberOne>3</TestNumberOne>
<TestNumberTwo>2</TestNumberTwo>
<TestNumberThree>-</TestNumberThree>
<TestResult>1</TestResult>
</TestCase1>
<TestCase3>
<TestNumberOne>3</TestNumberOne>
<TestNumberTwo>7</TestNumberTwo>
<TestNumberThree>*</TestNumberThree>
<TestResult>21</TestResult>
</TestCase3>
<TestCase2>
<TestNumberOne>2</TestNumberOne>
<TestNumberTwo>5</TestNumberTwo>
<TestNumberThree>/</TestNumberThree>
<TestResult>0.4</TestResult>
</TestCase2>
</ROOT>
------------------------------------------------------------------------
posted on 2011-05-04 15:10  ryhan  阅读(3127)  评论(0编辑  收藏  举报