Solved

Parse all XML in classic ASP

Posted on 2014-01-16
20
3,232 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
  • 6
  • 6
  • 4
  • +1
20 Comments
 
LVL 142

Expert Comment

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

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
 
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 32

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 32

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 32

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 142

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 32

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 32

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now