• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Trouble parsing data from some xml site

shahin

Active Member
I've written a script to parse some xml content using vba in combination with xpath. The first script I've pasted below with a site link in it is just working like magic. However, applying the same logic in another site I neither get any data nor any error. As it is my first time to work with any xml webpage, I can't understand any mistake I'm making. Thanks in advance for any input.

Here is the first script: (the working one)

Code:
Sub XML_Parsing()
    Dim http As New XMLHTTP60
    Dim xmldoc As Object, post As Object

    With http
        .Open "GET", "http://wservice.viabicing.cat/v1/getstations.php?v=1", False
        .send
        Set xmldoc = .responseXML
        xmldoc.LoadXML .responseXML.XML
    End With

     For Each post In xmldoc.SelectNodes("//station")
        x = x + 1: Cells(x, 1) = post.SelectNodes(".//lat")(0).Text
        Cells(x, 2) = post.SelectNodes(".//long")(0).Text
    Next post
End Sub

The second script which is not working at all:

Code:
Sub XML_Parsing()
    Dim http As New XMLHTTP60
    Dim xmldoc As Object, post As Object

    With http
        .Open "GET", "https://drinkup.london/sitemap.xml", False
        .send
        Set xmldoc = .responseXML
        xmldoc.LoadXML .responseXML.XML
    End With

     For Each post In xmldoc.SelectNodes("//url/loc")
        x = x + 1: Cells(x, 1) = post.Text
    Next post
End Sub

Any data from this site will do.
 
Found the solution already. Creating late binding does the trick:
Code:
Sub XML_Parsing_ano()
    Dim http As New XMLHTTP60
    Dim xmldoc As Object, post As Object

    With http
        .Open "GET", "https://drinkup.london/sitemap.xml", False
        .send
        Set xmldoc = CreateObject("MSXML2.DOMDocument")
        xmldoc.LoadXML .responseXML.XML
    End With

     For Each post In xmldoc.SelectNodes("//url/loc")
        x = x + 1: Cells(x, 1) = post.Text
    Next post
End Sub
 
In your last code you've initialized the object by...
Code:
Set xmldoc = CreateObject("MSXML2.DOMDocument")

However, in previous code, you only declared it as object and tried to load data to it without initializing it as specific object type.

Code:
 Set xmldoc = .responseXML

You either have to declare specific object "New MSXML2.DOMDocument" at Dim stage or Set stage.

Edit: FYI - Late Binding = When you initialize the object at Set stage. Early Binding = When you initialize the object at Dim stage. When you reference external library, both methods are available. If you don't, for external process, you can only use late binding method.
 
Last edited:
@Sir Chihiro, thanks for the clarity. Yes it rocks now as I expected in the first place. Now, it makes sense.
 
Back
Top