【问题标题】:How to get specific data from a tag from XML page?如何从 XML 页面的标签中获取特定数据?
【发布时间】:2019-01-06 06:44:17
【问题描述】:

我有以下代码从 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 站点中,但这具有类似的结构。

以下 FIELD 标记在上述 URL 中不可用。我以 url 为例来展示文档的类型。

假设页面中间有一个如下标签:

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

我怎样才能只得到这个“097cf4a8-2755-4c62-939c-9402e0a4e3e2”str2 是独一无二的。

【问题讨论】:

  • 这是&lt;FIELD NAME="str2"&gt;标签出现在文档中的唯一位置,还是有几个str2的?
  • 我没有在变量newstring中找到字符串“FIELD NAME”。!!
  • @srn 使用 Xpath

标签: excel xml vba web-scraping


【解决方案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

来源:

输出:

【讨论】:

  • 很高兴能帮上忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-07
  • 1970-01-01
  • 1970-01-01
  • 2020-06-13
相关资源
最近更新 更多