I'm using a VB6 app to get data from a REST web service, I'm getting the correct results back from the web service but the data is returned in XML format, I've tried to get the data back in Json but without any luck. Is this possible in VB6 without using any third party tools ? The next part of my question is, I need to populate the date that is returned from the web service into a grid, what is the best way to read this data ? should I use a DOM object ? I haven't done anything like this before so any advice or help would be greatly appreciated.
JSON would be more work, since we don't have any support for that in Windows. We have lots of XML support via MSXML though: SAX, DOM, and even the XML Simple Provider.
Which one fits best depends entirely on your situation. You want to display data in some sort of grid but that's only practical of the data is tabular in nature.
Without knowing more it is hard to recommend anything. We don't know what the XML looks like, how large it is, or how rapidly you need to process it. We don't know whether you are willing to spend RAM like a drunken sailor or your application needs a small footprint.
Option Explicit
Private Sub Form_Load()
Dim RS As ADODB.Recordset
Set RS = New ADODB.Recordset
With RS
.Open "Books.xml", _
"Provider=MSDAOSP;Data Source=MSXML2.DSOControl.3.0", _
, _
adLockReadOnly
Set MSHFlexGrid1.DataSource = RS
.Close
End With
MSHFlexGrid1.ColWidth(0) = 240
End Sub
Private Sub Form_Resize()
If WindowState <> vbMinimized Then
MSHFlexGrid1.Move 0, 0, ScaleWidth, ScaleHeight
End If
End Sub
Thank you for your reply and clearing up my question about json. The data I'm getting back from the web service will be used for reporting purposes so some data could be quite large. I've attached a .txt file with a sample of what the data looks like. Below is the code I'm using to send requests to the web service, the "ApiResult" string variable is what stores the response from the web service, i'll need to make sense of this variable to populate the grid for reporting.
Code:
Public Function WebRequest(Url As String) As String
Dim objHTTP As Object
Dim Json As String
Dim Result As String
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
objHTTP.Open "GET", Url, False
objHTTP.setRequestHeader "Content-type", "application/json"
objHTTP.sEnd
Result = objHTTP.responseText
ApiResult = Result
Set objHTTP = Nothing
End Function
If the web service offers a different call that provides XML responses then use that.
Actually, it's the the Accept header that is sent by the client that usually determines the format ot the output generated.
E.g. a RESTful service impl GET verb on /users end-point which interprets Accept: application/json vs Accept: application/xml header and sends results in JSON vs XML.
Usually RESTful web service frameworks implement this feature automagically because before getting to JSON/XML/HTML/CSV the results of a service call are usually in-memory objects (so called POCOs/POJOs) that just get serialized to different Content-Type.
Thank you for your reply, I have changed the vb6 code to the samples below but I still only get json back. Am I correct in in thinking that this is on the web api side and not the VB6 call ?
FYI, Content-type header is to the input you send the same as Accept header is to the result your get back, i.e. if you POST data to the service then the format of the data in request body is in Content-type header and at the same time you might specify in Accept header the format of the result you'd like to be returned in JSON/XML/whatever, so you have to set *both* headers when posting parameters in the body of your request (objHTTP).
If request parameters all are in the URL e.g. GET /users/12/owner/34 then Content-type header of your objHTTP is irrelevant as there is no body you send. Just use Accept header to receive XML in response.
Thanks so much, the "Accept" did the trick and I'm now getting the result back in XML.
I've attached the XMl sample in the message, I need to populate the response data into a grid so that I can build up a report. What would be the best method of achieving this ?
Please re-read the thread from the start, now that you have some XML back from the service.
In post 3 dilettante already provided a sample code how to bind XML data to a flex grid control through an ADODB.Recordset. This might need some tweaking to get it working with your particular web service response.