dcsimg
Results 1 to 8 of 8
  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    4

    Question How to parse an XML string?

    Hello, I have been looking but haven't been able to figure it out any help is much appreciated.
    I'm using access 2016, I was able to call a API and get a response in the form of a string, I need to be able to get an individual node.

    Private Sub Test_Click()

    Dim xmlhttp As New MSXML2.XMLHTTP60, myurl As String
    myurl = "https://api.data24-7.com/v/2.0?api=T&user=UserName&pass=MyPassword&p1=18008008001"
    xmlhttp.Open "GET", myurl, False
    xmlhttp.SEND
    MsgBox (xmlhttp.responseText)

    End Sub

    By using the above call I get:

    <?xml version= "1.0"?><response><results><result item="1"><Status>OK</Status><number>18008008001</number><wless>y</wless><carrier_name>T-Mobile................. </result></results></response>

    I need to be able to get one of the nodes, for example <carrier_name>
    If you could help me that would be fantastic, I haven't been able to figure it out.
    Best.

  2. #2
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,968

    Re: How to parse an XML string?

    What have you tried?

    Typically we use msxml to parse xml. https://msdn.microsoft.com/en-us/library/aa468547.aspx
    Imagine what it would be like to set breakpoints in, or step through subclassing code;
    and then being able to hit stop/end/debug or continue, without crashing the IDE.

    VB6.tlb | Bulletproof Subclassing in the IDE (no thunks/assembly/DEP issues)

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,148

    Re: How to parse an XML string?

    Since it's Access, it should be in VBA, though I believe the question thus far is viable as either VBA or VB6.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    4

    Re: How to parse an XML string?

    Hi, yes I did read about the XML DOM, but that seems to be about and actual xml file, I need to parse the response that I already got.
    I'm using MS access so I think its Vb6.
    Any pointers or direction or additional references are appreciated.
    Thanks.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,644

    Re: How to parse an XML string?

    Quote Originally Posted by HelloItsMe View Post
    Hi, yes I did read about the XML DOM, but that seems to be about and actual xml file, I need to parse the response that I already got.
    I'm using MS access so I think its Vb6.
    Any pointers or direction or additional references are appreciated.
    Thanks.
    It can be used with a file (using .Load) or with an XML String (using .LoadXML) ... as long as the XML is valid, you can load it either way.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,968

    Re: How to parse an XML string?

    not only that, but the .responseXML property of an MSXML2.XMLHTTP60 should return a parsed Document.
    Imagine what it would be like to set breakpoints in, or step through subclassing code;
    and then being able to hit stop/end/debug or continue, without crashing the IDE.

    VB6.tlb | Bulletproof Subclassing in the IDE (no thunks/assembly/DEP issues)

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    4

    Re: How to parse an XML string?

    Ok I got this far, I think that the answer is in the DisplayNode Sub, I'm looking at it now, I just need to get the node I want from the loop.

    Private Sub Command51_Click()

    Dim xmlhttp As New MSXML2.XMLHTTP60, myurl As String
    myurl = "https://api.data24-7.com/v/2.0?api=T&user=USER&pass=Password&p1=180080010"
    xmlhttp.Open "GET", myurl, False
    xmlhttp.Send
    MsgBox (xmlhttp.responseText)
    Dim xDoc As New MSXML2.DOMDocument60
    xDoc.loadXML (xmlhttp.responseText)
    DisplayNode xDoc.childNodes, 0

    End Sub

    Private Sub DisplayNode(Nodes, Indent)

    Dim xNode As MSXML2.IXMLDOMNode
    Indent = Indent + 2

    For Each xNode In Nodes
    If xNode.nodeType = NODE_TEXT Then
    Debug.Print Space$(Indent) & xNode.parentNode.nodeName & _
    ":" & xNode.nodeValue
    End If

    If xNode.hasChildNodes Then
    DisplayNode xNode.childNodes, Indent
    End If
    Next xNode
    End Sub

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    4

    Re: How to parse an XML string?

    Thanx All, I was able to get it using this: https://msdn.microsoft.com/en-us/library/aa468547.aspx
    and the .loadXML
    and .parentNode.nodeName

    This is awesome.
    Thank you again for the references and pointers.

    BEST.

    NOTE, I have to add that the examples out there use Microsoft XML, version 2.0 which is no longer available in Access 2016.
    Version 6 is used and the calls for the references are different:
    use this for newer applications:

    MSXML2.XMLHTTP60
    MSXML2.DOMDocument60
    MSXML2.IXMLDOMNode
    Last edited by HelloItsMe; Feb 23rd, 2018 at 04:49 PM.

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


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.