How to read web service XML on VB6?

Asked

Viewed 1,920 times

1

  • 1

    Example: https://technet.microsoft.com/pt-br/library/2008.10.heyscriptingguy.aspx

  • Example: https://stackoverflow.com/questions/8254600/reading-xml-file-with-vbscript

  • 1

    Thanks for the links Virgilio, but did not work.

  • @William, thank you very much. It worked, I will study your code. But I took a test here and responded. Thank you very much

  • @Can Renato mark the answer as correct then? If you do not know how to do this view this link https://pt.meta.stackoverflow.com/q/1078/3635, but only check if you are sure that the answer has even solved the specified problem. See you around.

1 answer

0


You can use XMLHTTPRequest, that works well similar to that of and to parse XML can use:

Set doc = CreateObject("MSXML2.DOMDocument")
doc.loadXML(xhr.responseText)

And Xpath can be useful for selecting the necessary nodes:

Set nodes = doc.SelectNodes("//IPAddress")

An example "synchronous":

Dim xhr, method, url, contents, formatcontent, doc

Set xhr = CreateObject("MSXML2.XMLHTTP")

method = "GET" 'Escolhe o método HTTP de envio
url = "https://ws.printwayy.com/api/Printer?api_token=1F61D333-CCA5-423A-A764-F8577119A9FE&company_token=&serialNumbers=AK18054352&initialDate=&endDate=" 'url da API
contents = "" 'conteudo
formatcontent = "application/json" 'Se a API usar outro formato basta alterar aqui

xhr.Open method, url, False

'Necessário pra sua API retornar XML ao invés de JSON
xhr.setRequestHeader "Accept", "application/xml"

If method = "POST" Or method = "PUT" Then
    xhr.setRequestHeader "Content-Type", formatcontent
    xhr.setRequestHeader "Content-Length", Len(contents)
    xhr.send contents
Else
    xhr.send
End If

If xhr.status < 200 Or xhr.status >= 300 Then
    'Algo falhou, as vezes pode haver uma descrição em `xhr.responseText` ou pode retornar vazio, o `xhr.status` indica o tipo de erro
    MsgBox "Erro HTTP:" & xhr.status & " - Detalhes: " & xhr.responseText
Else
    'Faz o parse da String para XML
    Set doc = CreateObject("MSXML2.DOMDocument")
    doc.loadXML(xhr.responseText)

    'Seleciona com XPATH
    Set nodes = doc.SelectNodes("//IPAddress")

    MsgBox "Elementos encontrados para IPAddress: " & nodes.length

    For Each node In nodes
        MsgBox "Endereço IP: " & node.text
    Next
End If

Note that I used MSXML2.XMLHTTP and MSXML2.DOMDocument if error occurs try changing the MSXML2. for Microsoft., depends on the system.


Asynchronous xmlhttprequest

To use asynchronous XMLHTTP you can use:

Set xhr = CreateObject("MSXML2.XMLHTTP")
xhr.open hMethod, hUrl, True

And you must use the property:

xhr.onreadystatechange = GetRef([Nome de uma Function ou Sub])

A complete example:

Dim hMethod, hUrl, hFormat, hContents, hAccepts, xhr

'url da API
hUrl = "https://ws.printwayy.com/api/Printer?api_token=1F61D333-CCA5-423A-A764-F8577119A9FE&company_token=&serialNumbers=AK18054352&initialDate=&endDate="

hMethod   = "GET"              'Metodo HTTP
hAccepts  = ""                 'Necessário pra sua API retornar XML ao invés de JSON
hContents = ""                 'Conteúdo em requisições POST/PUT
hAccepts  = "application/xml"  'http accepts

MsgBox hUrl

Set xhr = CreateObject("MSXML2.XMLHTTP")

Sub doParseXml(xmlStr)
    'Faz o parse da String para XML
    Set doc = CreateObject("MSXML2.DOMDocument")
    doc.loadXML(xmlStr)

    'Seleciona com XPATH
    Set nodes = doc.SelectNodes("//IPAddress")

    MsgBox "Elementos encontrados para IPAddress: " & nodes.length

    For Each node In nodes
      MsgBox "Endereço IP: " & node.text
    Next
End Sub

'Recebe assincronamente o resultado
Sub doReadyStateChange()
    If xhr.readyState = 4 Then
        If xhr.status < 200 Or xhr.status >= 300 Then
            MsgBox "Erro HTTP:" & xhr.status & " - Detalhes: " & xhr.responseText
        Else
            doParseXml xhr.responseText
        End If
    End If
End Sub

xhr.onreadystatechange = GetRef("doReadyStateChange")

xhr.open hMethod, hUrl, True

If hAccepts <> "" Then
    xhr.setRequestHeader "Accept", hAccepts
End If

If hMethod = "POST" Or hMethod = "PUT" Then
    'Accpet HTTP request
    If hFormat = "" Then
        xhr.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    Else
        xhr.setRequestHeader "Content-Type", hFormat
    End If

    xhr.setRequestHeader "Content-Length", Len(hContents)
    xhr.send hContents
Else
    xhr.send
End If

Browser other questions tagged

You are not signed in. Login or sign up in order to post.