srn
srn

Reputation: 89

How to get specific data from a tag from XML page?

I have the following code to get the response text from an XML page, but it returns everything in the page:

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

The URL in myurl is for example only. The URL i'm trying to get is in an intranet site but this has a similar structure.

The following FIELD tag is not available in the URL above. I used the url as an example to show the type of document.

Let's say there's a tag like below in the middle of the page:

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

How do I get only this "097cf4a8-2755-4c62-939c-9402e0a4e3e2"? The str2 is unique.

Upvotes: 0

Views: 540

Answers (1)

QHarr
QHarr

Reputation: 84465

Here is an example using a publicly available document. The principle of selecting by combination of tag and attribute value is shown.

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

Source:

data

Output:

out

Upvotes: 1

Related Questions