Reading HTML with XPath

Ferdi24

Board Regular
Joined
Feb 26, 2012
Messages
133
Hi everyone,

I am trying to read HTML documents using XPath in VBA. AS an easy example: Get my IP address fom whatismyip.com.
The problem: The only Xpath query that returns anything is "/". Everything else just does not return anything and the NodeList remains empty.

Does anyone have an idea where I'm going wrong?

Code:
Sub GetData() 
Dim objhttp As Object 
Dim XMLDatei As String 
Dim ResultList As IXMLDOMNodeList 
Dim i As Integer 
Dim counter As Integer 
Dim xml As New DOMDocument 
Dim XPathQuery As String 
Dim URL As String 

XPathQuery = "//div[@id='ip']" 
URL = "http://www.whatismyip.com" 

Set objhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0") 
    objhttp.Open "GET", URL, False 
    objhttp.send ("") 

xml.Load xmlsource:=objhttp.responseText 
xml.setProperty Name:="SelectionLanguage", Value:="XPath" 

Set ResultList = xml.SelectNodes(XPathQuery) 
counter = ResultList.Length 

For i = 0 To counter 
    Debug.Print ResultList.Item(i).nodeName 
Next i 
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
First, ensure you have the Microsoft XML reference enabled (my version is v3.0). Once you have that enabled, your code should work, but if it doesn't you can write it like this:
Code:
Dim ResultList As MSXML2.IXMLDOMNode
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top