Recieve XML Data from URL

KernelK

Registered User.
Local time
Today, 15:16
Joined
Oct 3, 2006
Messages
173
I've looked through all the posts here for an answer but havnt found one that I can use, so sorry if you guys have answered this before and I just cannot locate it. I am trying to parse XML data that is retrieved from an URL. I found code on MSDN that pertains to VB but I cannot modify it to work in Access as Access does not use the onreadystatechange event. When stepping through the code the class module never executes a line, and if I change the async value to False to get the response processed, the oNode variable returns as nothing. Could you help me with correcting this code, or perhaps provide me help with the solution that you found? Thanks in advance.
MSDN Article: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/418fd944-9256-4e61-8d7b-5ea41b2ca3cc.asp
My modified code (Blank DB with 1 form [form1] that has 1 textbox and 1 button; and the class module (modified)):

'Form_Form1
Option Compare Database
Option Explicit

Public oHttpReq As XMLHTTP30
Public oXMLDoc As DOMDocument30

Private Sub MakeRequest(ByVal isAsync As Boolean)
Set oHttpReq = New XMLHTTP30
Dim xhrHandler As myHttpRequestHandlers
Dim url As String

If isAsync = True Then
Set xhrHandler = New myHttpRequestHandlers

' Set a readyStateChange handler.
oHttpReq.OnReadyStateChange = xhrHandler
End If

' Construct the URL from user input.
url = "http://synergidev/api/api.vsd.php?q=users"

' Open a connection and set up a request to the server.
oHttpReq.Open "GET", url, isAsync

' Send the request to the server.
oHttpReq.send

' In a synchronous call, we must call ProcessResponse. In an
' asynchronous call, the OnReadyStateChange handler calls
' ProcessResponse.
If isAsync = False Then
ProcessResponse
End If
End Sub
Public Sub ProcessResponse()
' Receive the response from the server.
Set oXMLDoc = oHttpReq.responseXML
Dim oNode

' Display the server response to the user.
Set oNode = oXMLDoc.selectSingleNode("//username")
If oNode Is Nothing Then
Me.txtBox = "Requested information not found."
Else
Me.txtBox = oNode.Text

End If
End Sub

Private Sub cmdButton_Click()
MakeRequest (True)
End Sub

Private Sub Form_Load()
Me.txtBox = ""
End Sub

'myHttpRequestHandlers
Option Compare Database
Option Explicit

Sub OnReadyStateChange()
If Forms("form1")!oHttpReq.readyState = 4 Then
Forms("form1").ProcessResponse
End If
End Sub
 
It is possible that there IS a valid XML object contained in the response, but that the element username is empty.

Try this:

Set a break point at the line:
Code:
Set oXMLDoc = oHttpReq.responseXML

Run the code. When the sub halts at the breakpoint, type the following line in the debug window and press [ENTER]:
Code:
?oHttpReq.responseText
This should tell you whether you are even getting an XML object in the response, and if you are, whether the element username is contained therein.
 
Thanks, now.....

Wow. I can't believe I overlooked something as simple as assuming my response was blank without checking it in the debugger, thanks. I am getting a valid xml response in my variable when I turn Async off. But, the node username is not empty, I'll have to look into how to reference it properly I guess. Thanks Alot!
 
Can't reference the nodes properly

Like I was saying in the previous post, the username node is not empty, but I cannot seem to reference it properly to get a value from it using the SelectSingleNode method of the DOMDocument30 object. I looked at a bunch of MSDN stuff on XPATH syntax and I've tried each context of the node with no positive results. Can someone please help me to reference any node out of this xml response and place the value in the text box? Thanks in advance.
Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<users>
   <user1>
      <id>124</id>
      <active>1</active>
      <deleted>0</deleted>
      <username>VSD</username>
      <email></email>
      <management_level>0</management_level>
   </user1>
   <user2>
      <id>125</id>
      <active>1</active>
      <deleted>0</deleted>
      <username>VSD Lead</username>
      <email></email>
      <management_level>2</management_level>
   </user2>
   <user3>
      <id>126</id>
      <active>1</active>
      <deleted>0</deleted>
      <username>VSD Supervisor</username>
      <email></email>
      <management_level>3</management_level>
   </user3>
</users>
 

Users who are viewing this thread

Back
Top Bottom