954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to Parse XML in ASP?

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

' Create new XML reader object
Dim xmlDoc
Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")

' Load the specified XML file (returns XML output)
xmlDoc.load("http://somesite.com/api/xml?action=login&login=jon@doe.com&password=foobar")

' Parse XML
if xmlDoc.parseError.errorcode <> 0 then
  ' oops error in xml
  Response.Write("XML Error...")
else
  ' get xml data
  Response.Write(xmlDoc.documentElement.childNodes(0).attributes.getNamedItem("code").nodeValue)
end if

my xml looks like this...

<?xml version="1.0" encoding="utf-8"?>
<results>
	<status code="value"/>
</results>

and my error is...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

senexom
Junior Poster in Training
54 posts since Jun 2005
Reputation Points: 11
Solved Threads: 0
 

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

set xmlDoc = createObject("MSXML2.DOMDocument")

xmlDoc.async = False
xmlDoc.setProperty "ServerHTTPRequest", true
xmlDoc.load("http://somesite.com/api/xml?action=login&login=jon@doe.com&password=foobar")

response.write xmlDoc.selectSingleNode("//results/status").Attributes.GetNamedItem("code").Text

Cheers!

senexom
Junior Poster in Training
54 posts since Jun 2005
Reputation Points: 11
Solved Threads: 0
 

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

dude9er
Newbie Poster
2 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 
#
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>
  <data>
    <date20091231>
        <thumbnail url="xyz.jpg">
    </date20091231>
    <date20100101>
        <thumbnail url="abc.jpg">
    </date20100101>
  </data>
</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.

kapilgames2win
Newbie Poster
1 post since Dec 2009
Reputation Points: 10
Solved Threads: 0
 
#
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>
  <data>
    <date20091231>
        <thumbnail url="xyz.jpg">
    </date20091231>
    <date20100101>
        <thumbnail url="abc.jpg">
    </date20100101>
  </data>
</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.

set objXMLRoot = xmlDoc.selectSingleNode("//results/data")


Now u can handle this object and ask if it is empty:

if not (objXMLRoot is nothing) then
  for each objNode in objXMLRoot.childNodes()
   'Put what is needed in here
   response.write objNode.text
  next
end if


Conclusion:
Use objects to see if they are nothing.

GL,

Tai Kahar

Tai Kahar
Newbie Poster
1 post since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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.

sawone
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 0
 
Dim xmlDoc
xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.setProperty("ServerHTTPRequest", true)
xmlDoc.load("url_here")
dim objXMLRoot = xmlDoc.selectSingleNode("node_select_here")
dim objNode
if not (objXMLRoot is nothing) then
  for each objNode in objXMLRoot.childNodes()
   response.write(objNode.Text)
  next
end if


Only done quickly, hope it helps

jamziee
Newbie Poster
21 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

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

spence89
Light Poster
35 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

How dose this other page relate to the first page?

jamziee
Newbie Poster
21 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

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?

spence89
Light Poster
35 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Yh, i was in a simular situation. Can you post a example of the xml returned from the api

jamziee
Newbie Poster
21 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Of course,

Here is an example of the XML data returned. The messages will be different based on the variables posted to the asp page (obviously).

-2147220278


The username you have specified is already in use or invalid. Please type a different account number and try again.

spence89
Light Poster
35 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

give me a few mins to write somthing up

jamziee
Newbie Poster
21 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Thank you very much :)

spence89
Light Poster
35 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 
<%

dim xmlDoc
xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.setProperty("ServerHTTPRequest", true)
xmlDoc.load("http://localhost/data.xml")

dim request = xmlDoc.SelectNodes("//Results")
response.write(request(0).selectSingleNode("ResultCode").text)
response.write("")
response.write(request(0).selectSingleNode("ResultDescription").text)
response.write("")
response.write(request(0).selectSingleNode("ResultData").getAttribute("recordcount"))

%>


Change the http://localhost/data.xml to a location where you have a test xml file then preview this script in the browser, let me know if it helps and if there is any thing else i can help with :)

jamziee
Newbie Poster
21 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Thanks for that code.

I have a question though, the "data.xml. page, does that HAVE to be xml or can it contain other types?

The reason I ask is the ASP outputs XML, which means it does not have a XML extension. Does this matter or should I continue to enter the URL with ASP?

Also, the XML the API page produces only does so when I pass a variable to it. How would I go about submitting the variabled to this ASP page and then get the XML from the same ASP page?

Sorry if I am asking a lot, but there is nothing on the internet of this nature.

Again thanks for your help - it is very much appreciated.

spence89
Light Poster
35 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

How do you post the variable? and which variables do you need to submit to the api?

jamziee
Newbie Poster
21 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

I can post the variables using either a POST or GET form methods. These will come from a standard HTML form.

The variables are then passed to the API ASP page for processing, which will insert/update/delete records as necessary and then output the results in XML.

BUT the page still remains as an ASP page. There is never an XML document in site, just XML code on the ASP page which I need to transform back into HTML for display on the final ASP page.

So, Here's a small diagram of the process.

Normal HTML Form Page => Submit to ASP API Page which outputs XML => ASP page which turn the XML back into standard HTML for further processing.

I hope that's a little more helpful. Sorry to be a complete pain.

spence89
Light Poster
35 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

I need to know all the variables the html for submits and then the url where these variables need to get posted to

jamziee
Newbie Poster
21 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

See this page here: http://cp.silverlinehosting.co.uk/billing_api.asp

It should give you all the information you need.

Regards,
Adam

spence89
Light Poster
35 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You