Results 1 to 3 of 3

Thread: Using XPath with ID, not a path

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    1

    Using XPath with ID, not a path

    Hello,

    I'm trying to get some data from a website.

    When I get HTML code using:

    Code:
    Public Function GetHTMLCode(ByVal strURL) As String
    
        Dim iStrPos As Long
        Dim iStrEnd As Long
        On Error GoTo ErrorHandler
    
        Dim strError As String
        strError = ""
    
        Dim oXMLHTTP As MSXML2.XMLHTTP
        Set oXMLHTTP = New MSXML2.XMLHTTP
    
        Dim strResponse As String
        strResponse = ""
    
        With oXMLHTTP
                
            .Open "GET", strURL, False
            .send ""
            'If .Status <> 200 Then
            '    strError = .statusText
            '    GoTo CleanUpAndExit
            'Else
    
                'Debug.Print .getResponseHeader("Content-type")
    
                'If .getResponseHeader("Content-type") <> "text/html" Then
                '
                '    strError = "Not an HTML file"
                '    GoTo CleanUpAndExit
                'Else
                    GetHTMLCode = .responseText
                '    Debug.Print "GetHMLCode: downloaded " & Len(.responseText)
                'End If
    
            'End If
    
        End With
    
    
    CleanUpAndExit:
    
        On Error Resume Next ' Avoid recursive call to error handler
    
        ' Clean up code goes here
    
        Set oXMLHTTP = Nothing
    
        ' Report any error
    
        If Len(strError) > 0 Then
            MsgBox strError
        Else
          
    
        End If
    
        Exit Function
    ErrorHandler:
        strError = Err.Description
        Resume CleanUpAndExit
    
    
    End Function
    I'm able to extract data using getXPathElement function:

    Code:
    Public Function getXPathElement(sXPath As String, objElement As Object) As HTMLBaseElement
         Dim sXPathArray() As String
     
        Dim sNodeName As String
         Dim sNodeNameIndex As String
         Dim sRestOfXPath As String
         Dim lNodeIndex As Long
         Dim lCount As Long
     
        ' Split the xpath statement
         sXPathArray = Split(sXPath, "/")
         sNodeNameIndex = sXPathArray(1)
         If Not InStr(sNodeNameIndex, "[") > 0 Then
             sNodeName = sNodeNameIndex
             lNodeIndex = 1
         Else
             sXPathArray = Split(sNodeNameIndex, "[")
             sNodeName = sXPathArray(0)
             lNodeIndex = CLng(Left(sXPathArray(1), Len(sXPathArray(1)) - 1))
         End If
         sRestOfXPath = Right(sXPath, Len(sXPath) - (Len(sNodeNameIndex) + 1))
     
        Set getXPathElement = Nothing
         For lCount = 0 To objElement.ChildNodes().Length - 1
             If UCase(objElement.ChildNodes().item(lCount).nodeName) = UCase(sNodeName) Then
                 If lNodeIndex = 1 Then
                     If sRestOfXPath = "" Then
                         Set getXPathElement = objElement.ChildNodes().item(lCount)
                     Else
                         Set getXPathElement = getXPathElement(sRestOfXPath, objElement.ChildNodes().item(lCount))
                     End If
                 End If
                 lNodeIndex = lNodeIndex - 1
             End If
         Next lCount
     End Function
    as for example:

    Code:
    Dim elem As HTMLBaseElement
    Dim oHTML As New HTMLDocument
    ...
    oHTML.body.innerHTML = GetHTMLCode(sUrl)
    Set elem = getXPathElement("/html/body/div[2]/div/div[2]/div[6]/div/div/div/div[" & i & "]/div/div[2]/ul", oHTML)
    Debug.Print elem.InnerText
    Everything works well.

    But I need to extract data by providing unique ID, something like:
    Code:
    //div[@id='prices']/table/tbody/tr[2]/td[5]
    I know it works for XML in VB.NET with MSXML2.DOMDocument.4.0 library, but I need to implement it for HTML/XHTML in VBA (Excel).

    Any ideas how to use xpath to get i.e. div[@id='prices'] ?

  2. #2
    New Member
    Join Date
    Aug 2017
    Posts
    10

    Re: Using XPath with ID, not a path

    Hi derlee,

    did you solve your issue?

    I am having the same problem.

    HTMLBaseElement is for waht library? How can i set this up ?

    Best,
    Jacek

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,469

    Re: Using XPath with ID, not a path

    derlee made 1 post in 2013, i doubt he will be watching this thread

    HTMLBaseElement is for waht library
    probably microsofthtml object library
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width