943,865 Members | Top Members by Rank

Ad:
  • ASP Discussion Thread
  • Unsolved
  • Views: 80724
  • ASP RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 8th, 2005
0

How to Parse XML in ASP?

Expand Post »
Hey guys,
I'm having a "great" pleasure of parsing XML in my ASP page, except I can't seem to figure it out what I'm doing wrong. I have tried a bunch of stuff and can't seem to read a simple xml doc. here is what my code looks like...
ASP Syntax (Toggle Plain Text)
  1. ' Create new XML reader object
  2. Dim xmlDoc
  3. Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
  4.  
  5. ' Load the specified XML file (returns XML output)
  6. xmlDoc.load("http://somesite.com/api/xml?action=login&login=jon@doe.com&password=foobar")
  7.  
  8. ' Parse XML
  9. if xmlDoc.parseError.errorcode <> 0 then
  10. ' oops error in xml
  11. Response.Write("XML Error...<br>")
  12. else
  13. ' get xml data
  14. Response.Write(xmlDoc.documentElement.childNodes(0).attributes.getNamedItem("code").nodeValue)
  15. end if

my xml looks like this...
ASP Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <results>
  3. <status code="value"/>
  4. </results>

and my error is...
Quote ...
Microsoft VBScript runtime error '800a01a8'

Object required: 'documentElement'
This is my first time working with XML in ASP, so it is possible i'm missing something simple, any ideas what's going wrong here?

Thanks
Similar Threads
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
senexom is offline Offline
54 posts
since Jun 2005
Aug 9th, 2005
1

Re: How to Parse XML in ASP?

I've figured it out and just in case someone else is forced to use ASP to process XML...

The code above WORKS on an XML document on the server, but if you are like me and you're trying to read XML document from a response stream the following code does the trick...

ASP Syntax (Toggle Plain Text)
  1. set xmlDoc = createObject("MSXML2.DOMDocument")
  2.  
  3. xmlDoc.async = False
  4. xmlDoc.setProperty "ServerHTTPRequest", true
  5. xmlDoc.load("http://somesite.com/api/xml?action=login&login=jon@doe.com&password=foobar")
  6.  
  7. response.write xmlDoc.selectSingleNode("//results/status").Attributes.GetNamedItem("code").Text
Cheers!
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
senexom is offline Offline
54 posts
since Jun 2005
Jun 29th, 2007
0

Re: How to Parse XML in ASP?

I am having to send a form to a 3rd party server, the server will respond with a XML validation and policy number. I need to then parse this XML response with ASP into my own webpage.

The info you have given so far seems to be what I need but I can't get it to work. Can you give more information on what you did to parse the XML, including ASP and XML code samples?

Much appreciated!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dude9er is offline Offline
2 posts
since Jun 2007
Dec 31st, 2009
0
Re: How to Parse XML in ASP?
asp Syntax (Toggle Plain Text)
  1. #
  2. response.write xmlDoc.selectSingleNode("//results/status").Attributes.GetNamedItem("code").Text

If node "status" is not present in xml, it will throw an error in asp and whole page is not displayed. Is there any way to handle that error in asp.

I need to display contents according to date so my xml is like
xml Syntax (Toggle Plain Text)
  1. <xml>
  2. <data>
  3. <date20091231>
  4. <thumbnail url="xyz.jpg">
  5. </date20091231>
  6. <date20100101>
  7. <thumbnail url="abc.jpg">
  8. </date20100101>
  9. </data>
  10. </xml>

Now On 2nd jan, thumbnail won't be found. In that case, I need a default thumbnail to be shown but since tag is not present, I'm getting an error.

Thanks in advance.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kapilgames2win is offline Offline
1 posts
since Dec 2009
Jan 21st, 2010
0
Re: How to Parse XML in ASP?
asp Syntax (Toggle Plain Text)
  1. #
  2. response.write xmlDoc.selectSingleNode("//results/status").Attributes.GetNamedItem("code").Text

If node "status" is not present in xml, it will throw an error in asp and whole page is not displayed. Is there any way to handle that error in asp.

I need to display contents according to date so my xml is like
xml Syntax (Toggle Plain Text)
  1. <xml>
  2. <data>
  3. <date20091231>
  4. <thumbnail url="xyz.jpg">
  5. </date20091231>
  6. <date20100101>
  7. <thumbnail url="abc.jpg">
  8. </date20100101>
  9. </data>
  10. </xml>

Now On 2nd jan, thumbnail won't be found. In that case, I need a default thumbnail to be shown but since tag is not present, I'm getting an error.

Thanks in advance.
Hi,

there are several ways, but first of all, u need to create a node-object.

ASP Syntax (Toggle Plain Text)
  1. set objXMLRoot = xmlDoc.selectSingleNode("//results/data")

Now u can handle this object and ask if it is empty:
ASP Syntax (Toggle Plain Text)
  1. if not (objXMLRoot is nothing) then
  2. for each objNode in objXMLRoot.childNodes()
  3. 'Put what is needed in here
  4. response.write objNode.text
  5. next
  6. end if

Conclusion:
Use objects to see if they are nothing.

GL,

Tai Kahar
Last edited by Tai Kahar; Jan 21st, 2010 at 1:45 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tai Kahar is offline Offline
1 posts
since Jan 2010
Mar 15th, 2010
0
Re: How to Parse XML in ASP?
Thanks for posting this code. I'm having an issue with the "set" command.

Did you end up using a revised version of the post?

I'm attempting to process XML data returned from the USPS Merchandise Return API.

Any direction would be appreciated. When I plug the code into my ASP page, the page reports an error (due to the "set" command use, as best I can tell).

Thanks in advance.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sawone is offline Offline
1 posts
since Mar 2010
Jun 11th, 2010
0

Hope it helps

ASP Syntax (Toggle Plain Text)
  1. Dim xmlDoc
  2. xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
  3. xmlDoc.async = False
  4. xmlDoc.setProperty("ServerHTTPRequest", true)
  5. xmlDoc.load("url_here")
  6. dim objXMLRoot = xmlDoc.selectSingleNode("node_select_here")
  7. dim objNode
  8. if not (objXMLRoot is nothing) then
  9. for each objNode in objXMLRoot.childNodes()
  10. response.write(objNode.Text)
  11. next
  12. end if

Only done quickly, hope it helps
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jamziee is offline Offline
21 posts
since Jun 2010
Jun 17th, 2010
0
Re: How to Parse XML in ASP?
Hi guys,

I am too having troubles with this. I have a ASP page which handles datat posted to it, and outputs XML.

My problem is this, when I pass data to the ASP page, how can I then display the XML data on another ASP page?

Any ideas would be VERY welcome.

Kind Regards,
Adam
Reputation Points: 10
Solved Threads: 0
Light Poster
spence89 is offline Offline
35 posts
since Jun 2010
Jun 17th, 2010
0
Re: How to Parse XML in ASP?
How dose this other page relate to the first page?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jamziee is offline Offline
21 posts
since Jun 2010
Jun 17th, 2010
0
Re: How to Parse XML in ASP?
Basically I am using a API system.

What happens is that when I pass variables to this API page it will insert a record into the database and then output the status in XML.

I need to be able to read this XML data after I submit the variables and display it on another asp page.

Am I making sense?
Reputation Points: 10
Solved Threads: 0
Light Poster
spence89 is offline Offline
35 posts
since Jun 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP Forum Timeline: Structure of for loop
Next Thread in ASP Forum Timeline: Simple associative array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC