Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract XML file from URL

Status
Not open for further replies.

whodaman

Programmer
May 23, 2001
60
CA
I have found code to use the MS XML parser 3.0 as you can see below. What I want to do this be able to load and parse the XML file from a given URL. I simiply gave the .load function a URL string. But this does not work. How do I go about this?

Code:
Private Sub Form_Load()
  Dim objDoc As MSXML2.DOMDocument30
  Dim objNodeList As MSXML2.IXMLDOMNodeList
  Dim objNode As MSXML2.IXMLDOMNode
        
  Set objDoc = New MSXML2.DOMDocument30
   
  If objDoc.Load("[URL unfurl="true"]http://www.someserver.com/customerdata.xml")[/URL] Then
    If Not objDoc Is Nothing Then
      Set objNodeList = objDoc.selectNodes("customerdata/customer/firstname")
            
      If Not objNodeList Is Nothing Then
         'Loop though each node in the node list
         For Each objNode In objNodeList
          Text1.Text = Text1.Text + vbCrLf + objNode.Text
        Next
      End If
    End If
  End If
End Sub
 
Microsoft-documentation says about the Load()-method:

Boolean. Returns True if the load succeeded; False if the load failed.
If the URL cannot be resolved or accessed or does not reference an XML document, this method returns an error and sets the documentElement property of the DOMDocument to Null.


However, I just gave it a try: first time my firewall blocked the site, second time I used a non-existing URL.
In both cases objDoc.Load(...) returned 'true', no errors raised, although objDoc.documentElement was Nothing.

So: better add an extra check:

if objDoc.Load(...) then
if not objDoc.documentElement is Nothing then
 
I wrote added the extra check, gave it a working URL to customerdata.xml, and objDoc.documentElement is still nothing.

I'm not sure what is wrong. Did your code work?

Thanks
 
Wow this is getting weird.
First time I just stepped through code, and it worked fine.
This time, I ran the same code and it didn't work.
Look at this:
Code:
Public Sub Main()
Dim objDoc As New MSXML2.DOMDocument30

If objDoc.Load("[URL unfurl="true"]http://www.w3schools.com/xml/note.xml")[/URL] Then
retry:
    If objDoc.documentElement Is Nothing Then
        Debug.Print "What the #$#@? no error, still nothing loaded?"
    Else
        Debug.Print objDoc.xml
    End If
Else
    Debug.Print "Can't load xml"
End If

Stop
GoTo retry

End Sub
When I continue after Stop, I actually have this debug-output:
What the #$#@? no error, still nothing loaded?
<?xml version="1.0"?>
<!-- Edited with XML Spy v4.2 -->
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
So, DOM needs a moment to think things over????

We'll need help from the big boys here!
 
I noticed that if I put a break on the test line below, the file loads correctly and it works. What do you think is wrong? Do I have to put a timer there? I tried Sleep, but it doesn't work.

Code:
if not objDoc.documentElement is Nothing then

 
I noticed exactly the same thing, and I don't have a clue...
Good to know that there are people on this forum that know a zillion times as much as I do...
 
GOT IT !!
(I should read documentation more carefully...)
Code:
Dim objDoc As New MSXML2.DOMDocument30
[b]
objDoc.async = False
[/b]
If objDoc.Load("[URL unfurl="true"]http://www.w3schools.com/xml/note.xml")[/URL] Then
 
Thank you so much! Good job... here's the code for anyone's information:

Code:
Private Sub readXMLFile()
  Dim objDoc As MSXML2.DOMDocument30
  Dim objNodeList As MSXML2.IXMLDOMNodeList
  Dim objNode As MSXML2.IXMLDOMNode
        
  Set objDoc = New MSXML2.DOMDocument30
  
  objDoc.async = False
  If objDoc.Load("[URL unfurl="true"]http://www.w3schools.com/xml/note.xml")[/URL] Then
  
    If Not objDoc.documentElement Is Nothing Then
        If Not objDoc Is Nothing Then
          Set objNodeList = objDoc.selectNodes("note/body")
                
          If Not objNodeList Is Nothing Then
                
             'Loop though each node in the node list
             For Each objNode In objNodeList
              Text1.Text = Text1.Text + vbCrLf + objNode.Text
            Next
          End If
        End If
    End If
  End If
End Sub

given the xml file:
Code:
<?xml version="1.0"?>
<!-- Edited with XML Spy v4.2 -->
<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top