-
Feb 22nd, 2018, 06:21 PM
#1
Thread Starter
New Member
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.
-
Feb 23rd, 2018, 08:39 AM
#2
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
-
Feb 23rd, 2018, 11:45 AM
#3
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
-
Feb 23rd, 2018, 12:36 PM
#4
Thread Starter
New Member
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.
-
Feb 23rd, 2018, 02:16 PM
#5
Re: How to parse an XML string?
Originally Posted by HelloItsMe
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
-
Feb 23rd, 2018, 03:08 PM
#6
Re: How to parse an XML string?
not only that, but the .responseXML property of an MSXML2.XMLHTTP60 should return a parsed Document.
-
Feb 23rd, 2018, 05:03 PM
#7
Thread Starter
New Member
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
-
Feb 23rd, 2018, 05:25 PM
#8
Thread Starter
New Member
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 05: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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|