Home > OS >  Parse line from XML response in Excel VBA
Parse line from XML response in Excel VBA

Time:11-30

I am looking to parse a 'WinHttpRequest' response in Excel VBA to pull a specific line from the XML response. Here is the code for the HTTP request.

Function getSetName(ByVal setNUMBER As String) As String
    Dim oRequest As Object
    Dim xmlResponse As String
    Dim setDescription As String
    Dim setName As String
   

    Set oRequest = CreateObject("WinHttp.WinHttpRequest.5.1")

    oRequest.Open "GET", "https://brickset.com/sets/75192" '& setNUMBER
    oRequest.Send
    xmlResponse = oRequest.ResponseText
    
    'parse xml response here
    
    getSetName = setName

End Function

I am looking to parse only the line with the HTML tag 'meta name=""description""' to a string which I will later pull information from.

Any help or direction on how to parse this single line would be appreciated.

CodePudding user response:

Try

Sub Test_GetSetName()
    Debug.Print GetSetName("75192")
End Sub

Function GetSetName(ByVal SetNUMBER As String) As String
    Dim html As New MSHTML.HTMLDocument
    With CreateObject("WinHttp.WinHttpRequest.5.1")
        .Open "GET", "https://brickset.com/sets/" & SetNUMBER, False
        .send
        html.body.innerHTML = .responseText
    End With
    GetSetName = html.querySelector("[name='description']").Content
End Function
  • Related