码迷,mamicode.com
首页 > 编程语言 > 详细

VBA_Xpath定位元素

时间:2020-05-11 22:00:57      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:xmldom   body   定位   new   路径   des   ror   int   out   

 1 Sub Xpath定位() MSXML2.DOMDocument.6.0
 2     Const str_Html As String = _
 3         "<div id = ""outside"">" & _
 4             "<div id =""box"">" & _
 5                 "<div id = ""head"" class = ""color0"">AAA</div>" & _
 6                 "<div id = ""body"" class = ""color1"">BBB</div>" & _
 7                 "<div id = ""footer1"" class = ""color1"">" & _
 8                     "<div id = ""footer2"" class = ""color2"">CCC</div>" & _
 9                 "</div>" & _
10             "</div>" & _
11         "</div>"
12     Dim xmlDom As New MSXML2.DOMDocument60  引用Microsoft xml,v6.0
13     Dim xmlDom As Object
14     Set xmlDom = CreateObject("Msxml2.DOMDocument.6.0")
15                                           
16     If Not xmlDom.LoadXML(str_Html) Then
17         MsgBox xmlDom.parseError.reason
18         Set xmlDom = Nothing
19         Exit Sub
20     End If
21     Debug.Print xmlDom.SelectSingleNode("//div/ @ id").Text 根路径下div id
22     Debug.Print xmlDom.SelectSingleNode("//div/div/ @ id").Text
23     Debug.Print xmlDom.SelectSingleNode("//div/div[ @ class = ""color1""]").Text
24     Debug.Print xmlDom.SelectSingleNode("//div/div[ @ id=""body""]").Text
25     Debug.Print xmlDom.SelectNodes("//div/div/@id")(0).Text
26     Debug.Print xmlDom.SelectNodes("//div/div/@id")(2).Text
27     Debug.Print xmlDom.SelectNodes("//div/div[@id=""footer2""]")(0).Text 只有一个结果
28     Debug.Print xmlDom.SelectNodes("//div [@id=""box""] /div [@class=""color1"" ] ")(0).Text
29     Debug.Print xmlDom.SelectNodes("//div/div/@id")(0).XML
30     
31     Set xmlDom = Nothing
32 End Sub

 

VBA_Xpath定位元素

标签:xmldom   body   定位   new   路径   des   ror   int   out   

原文地址:https://www.cnblogs.com/Ionefox/p/12872065.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!