如何从XML页面的标签中获取特定数据?

时间:2018-07-30 03:57:32

标签: excel xml vba web-scraping

我有以下代码从XML页面获取响应文本,但它返回了页面中的所有内容:

Private Sub Testing()
Dim xmlhttp As New MSXML2.XMLHTTP60, myurl As String
myurl = "http://schemas.xmlsoap.org/soap/envelope/"
xmlhttp.Open "GET", myurl, False
xmlhttp.send
newstring = xmlhttp.responseText
Sheet1.Range("B2") = newstring
End Sub

myurl中的URL仅作为示例。我要获取的URL在Intranet站点中,但这具有类似的结构。

以下URL标记不可用。我以网址为例来说明文档的类型。

假设在页面中间有一个如下所示的标签:

<FIELD NAME="str2">097cf4a8-2755-4c62-939c-9402e0a4e3e2</FIELD>

我怎么只得到这个“ 097cf4a8-2755-4c62-939c-9402e0a4e3e2” str2 是唯一的。

1 个答案:

答案 0 :(得分:1)

这里是使用公开文档的示例。给出了标签和属性值组合选择的原理。

Option Explicit
Public Sub Testing()
    Dim xmlhttp As New MSXML2.XMLHTTP60, myurl As String
    myurl = "http://www.chilkatsoft.com/xml-samples/bookstore.xml"
    xmlhttp.Open "GET", myurl, False
    xmlhttp.send
    Dim xmlDoc As New MSXML2.DOMDocument60
    Set xmlDoc = New MSXML2.DOMDocument60
    xmlDoc.LoadXML xmlhttp.responseText
    Debug.Print xmlDoc.SelectSingleNode("//userComment[@rating=""3""]").Text
End Sub

来源:

data

输出:

out

相关问题