|
Dim strXML
GetXml "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 |
|