Solved

Parse all XML in classic ASP

Posted on 2014-01-16
20
3,776 Views
Last Modified: 2014-02-06
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
Comment
Question by:whosbetterthanme
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 4
  • +1
20 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39786305
and the error is? which line?
what is the XML data?
0
 
LVL 33

Assisted Solution

by:Big Monty
Big Monty earned 250 total points
ID: 39786332
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39786378
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 7

Author Comment

by:whosbetterthanme
ID: 39789082
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
 
LVL 33

Expert Comment

by:Big Monty
ID: 39789102
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39789111
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
 
LVL 7

Author Comment

by:whosbetterthanme
ID: 39789145
<?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
 
LVL 7

Author Comment

by:whosbetterthanme
ID: 39789235
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
 
LVL 33

Expert Comment

by:Big Monty
ID: 39789254
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
 
LVL 7

Author Comment

by:whosbetterthanme
ID: 39789310
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
 
LVL 33

Expert Comment

by:Big Monty
ID: 39789317
just as a test, what if you change

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

to

set nodes = xmlDoc.selectNodes("//bookstore")
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39789422
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
 
LVL 7

Author Comment

by:whosbetterthanme
ID: 39789727
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
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
ID: 39790538
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
 
LVL 7

Author Comment

by:whosbetterthanme
ID: 39805084
Got it working. I think it was a permission problem on the XML file.
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 39805094
ummm, how is the accepted answer any different from the answer i suggested?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39810164
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
 
LVL 33

Expert Comment

by:Big Monty
ID: 39812299
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question