dcsimg
Results 1 to 15 of 15

Thread: [RESOLVED] XML result from wep api

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Location
    South Africa
    Posts
    400

    Resolved [RESOLVED] XML result from wep api

    Hi All

    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.

    Many thanks in advance.
    Aj.

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,069

    Re: XML result from wep api

    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.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,069

    Re: XML result from wep api

    Simple example:

    Code:
    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
    Attached Files Attached Files

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Location
    South Africa
    Posts
    400

    Re: XML result from wep api

    Hi dilettante

    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
    Many thanks again for your help on this.
    Attached Files Attached Files

  5. #5
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    2,035

    Re: XML result from wep api

    that _is_ JSON, not XML.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Location
    South Africa
    Posts
    400

    Re: XML result from wep api

    Hi DEXWERX

    Are you referring to the data in the text document ? The web api is only set to return XML.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Location
    South Africa
    Posts
    400

    Re: XML result from wep api

    you're 100% correct, the output is in JSON, do I have to change anything in the VB6 code to get the response back in XML ?

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,069

    Re: XML result from wep api

    If you have JSON you have to work with JSON. Id the web service offers a different call that provides XML responses then use that.

    Otherwise you'll want to use a JSON library. These come as both DLLs and source you can compile in-line. Here is one example:

    VB6 - JsonBag, Another JSON Parser/Generator

    You'll probably want the newest version there.

  9. #9
    Fanatic Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    716

    Re: XML result from wep api

    Quote Originally Posted by dilettante View Post
    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.

    cheers,
    </wqw>

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Location
    South Africa
    Posts
    400

    Re: XML result from wep api

    Thank you dilettante

    I'll have a look at that tool, I'm going to try and get the response back in XML as that would be easier to handle.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Location
    South Africa
    Posts
    400

    Re: XML result from wep api

    Hi wqweto

    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 ?

    Code:
    objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
    objHTTP.setRequestHeader "Content-type", "application/xml"
    many thanks.

  12. #12
    Fanatic Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    716

    Re: XML result from wep api

    Try *adding* objHTTP.setRequestHeader "Accept", "application/xml"

    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.

    cheers.
    </wqw>

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Location
    South Africa
    Posts
    400

    Re: XML result from wep api

    Hi wqweto

    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 ?

    Many thanks.XML.txt

  14. #14
    Fanatic Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    716

    Re: XML result from wep api

    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.

    cheers,
    </wqw>

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Location
    South Africa
    Posts
    400

    Re: XML result from wep api

    Thanks again to all for all the help and feedback on this post. I'm back on track.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width