• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4675
  • Last Modified:

Parse all XML in classic ASP

Using classic ASP, how do I simply open an XML file, and loop all the elements and print the node and values?

Using the code below doiesn't seem to work:

Set xmlDoc = Server.CreateObject("Msxml2.DOMDocument.6.0")

xmlDoc.async = "false"
xmlDoc.loadXML("test.xml")

set nodes = xmlDoc.selectNodes("//*") 

for i = 0 to nodes.length - 1
    response.write(nodes(i).nodeName & " - " & nodes(i).text & "<br />")
next

Open in new window

0
whosbetterthanme
Asked:
whosbetterthanme
  • 6
  • 6
  • 4
  • +1
2 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
and the error is? which line?
what is the XML data?
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
try this:

set nodes = oXML.selectNodes("//*")    
for i = 0 to nodes.length -1
    set attributes = nodes.item(i).attributes
    for j = 0 to attributes.length - 1
        response.write(attributes.item(j).nodeName & " - " & attributes.item(j).value & "<br />")
    next
next

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
Your first problem is that the loadXML() method is expecting a string of XML, rather than a filename. You need to use the load() method instead.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
whosbetterthanmeAuthor Commented:
Thanks all, but running the code below return no results:

Set xmlDoc = Server.CreateObject("Msxml2.DOMDocument.6.0")

xmlDoc.async = "false"
xmlDoc.load("test.xml")


set nodes = xmlDoc.selectNodes("//*")    
for i = 0 to nodes.length -1
    set attributes = nodes.item(i).attributes
    for j = 0 to attributes.length - 1
        response.write(attributes.item(j).nodeName & " - " & attributes.item(j).value & "<br />")
    next
next

Open in new window

0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
try adding this to your code right before you load the xml file:

xmlDoc.setProperty "ServerHTTPRequest", true

more info on that property can be found here
0
 
Carl TawnSystems and Integration DeveloperCommented:
Can you post the XML you are using?  The logic is fine, so it must be a problem either loading the XML or in the XML itself.
0
 
whosbetterthanmeAuthor Commented:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Edited by XMLSpy -->
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

Open in new window

0
 
whosbetterthanmeAuthor Commented:
I did verify that the file is able to be read by using:

set fs=Server.CreateObject("Scripting.FileSystemObject")


if fs.FileExists("D:\Inetpub\Wwwroot\test.xml") then

Response.Write("Found the XML File <br />")

Set xmlDoc = Server.CreateObject("Msxml2.DOMDocument.6.0")

xmlDoc.async = "false"
xmlDoc.load("D:\Inetpub\Wwwroot\test.xml")


set nodes = xmlDoc.selectNodes("//*")    
for i = 0 to nodes.length -1
    set attributes = nodes.item(i).attributes
    for j = 0 to attributes.length - 1
        response.write(attributes.item(j).nodeName & " - " & attributes.item(j).value & "<br />")
    next
next

Else
Response.Write("File doesn't exist")

End If

Open in new window

0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
did you try the parameter I offered above?

also, the load() method accepts only the relative path of the file, so it should be:

xmlDoc.load("/test.xml")
0
 
whosbetterthanmeAuthor Commented:
Here is what I have. The xml file is in the same directory as the script. It's weird as it won't return anything.

<%



Set xmlDoc = Server.CreateObject("Msxml2.DOMDocument.6.0")

xmlDoc.async = "false"
xmlDoc.setProperty "ServerHTTPRequest", true
xmlDoc.load("/test.xml")


set nodes = xmlDoc.selectNodes("//*")    
for i = 0 to nodes.length -1
    set attributes = nodes.item(i).attributes
    for j = 0 to attributes.length - 1
        response.write(attributes.item(j).nodeName & " - " & attributes.item(j).value & "<br />")
    next
next

	
	%>

	Done

Open in new window

0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
just as a test, what if you change

set nodes = xmlDoc.selectNodes("//*")  

to

set nodes = xmlDoc.selectNodes("//bookstore")
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
if the file is in the same folder, it should NOT be '/test.xml', but 'test.xml'

can you actually read the file contents using plain readfile?
http://www.php.net/manual/de/function.readfile.php
0
 
whosbetterthanmeAuthor Commented:
Running below read the contents fine. I also tried Big Monty's suggestion still no luck.

Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.OpenTextFile(Server.MapPath("test.xml"), 1)

do while f.AtEndOfStream = false
Response.Write(f.ReadLine)
Response.Write("<br>")
loop

f.Close
Set f=Nothing
Set fs=Nothing

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
This is the exact code I am using to test, and it is working fine (with your XML):
<%
   Dim oxml, nodes, attributes

   oxml = Server.CreateObject("MSXML2.DOMDocument.6.0")
   oxml.Load("C:\test.xml")

   'If err.Number <> 0 Then 
   '  Response.Write(err.Description)
   'End If

   'nodes = oxml.selectNodes("//book")
   'Response.Write(nodes.length)

   'For Each node in nodes
   '    Response.Write(node.nodeName & ": " & node.Text & "<br />")
   'Next

    nodes = oxml.selectNodes("//*")    
    for i = 0 to nodes.length -1
        attributes = nodes.item(i).attributes
        for j = 0 to attributes.length - 1
            response.write(attributes.item(j).nodeName & " - " & attributes.item(j).value & "<br />")
        next
    next

    oxml = Nothing
%>

Open in new window

0
 
whosbetterthanmeAuthor Commented:
Got it working. I think it was a permission problem on the XML file.
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
ummm, how is the accepted answer any different from the answer i suggested?
0
 
Carl TawnSystems and Integration DeveloperCommented:
My post was simply to demonstrate that there was nothing wrong with the code itself. However, the earlier post was printing out the values of the attributes, rather than the nodes, which wasn't what the asker originally wanted.

Secondly, part of the original problem was trying to load the XML with LoadXML, which wouldn't work when loading from a file.

Thirdly, given that the asker has stated that it was a permissions issue, then technically none of the suggestions posted were actually the solution to the issue, and the original code would have worked fine once the code was switched to use load() instead of loadXML()
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
actually, the code i posted (then you re-posted) printed out both the node name as well as the node value:

       response.write(attributes.item(j).nodeName & " - " & attributes.item(j).value & "<br />")
 

Open in new window


if it was a matter of using load() over loadXML(), that's fine, I'd just like the OP to verify that. Even if that was the case, the code still wouldn't have worked properly because the original code did not look in the ATTRIBUTES property to get get the right nodeName and value.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 6
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now