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...<br>")
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

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!

Comments
This code is useful to me

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

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

#
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

Edited 6 Years Ago by Tai Kahar: n/a

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.

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

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

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?

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

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


<Results>
<ResultCode>-2147220278</ResultCode>

<ResultDescription>
The username you have specified is already in use or invalid. Please type a different account number and try again.
</ResultDescription>
<ResultData recordcount="0"/>
</Results>

<%

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("<br>")
response.write(request(0).selectSingleNode("ResultDescription").text)
response.write("<br>")
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 :)

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.

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

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.

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

Set your form to post to this script and change the url to point towards the api, then let me know if this works

<%

dim FirstName = request.querystring("FirstName")
dim LastName = request.querystring("LastName")
dim PrimaryEmail = request.querystring("PrimaryEmail")
dim Address1 = request.querystring("Address1")
dim PostCode = request.querystring("PostCode")
dim CountryCode = request.querystring("CountryCode")
dim Title = request.querystring("Title")
dim CompanyName = request.querystring("CompanyName")
dim Address2 = request.querystring("Address2")
dim Address3 = request.querystring("Address3")
dim Town = request.querystring("Town")
dim County = request.querystring("County")
dim HomePhone = request.querystring("HomePhone")
dim WorkPhone = request.querystring("WorkPhone")
dim MobilePhone = request.querystring("MobilePhone")
dim FaxNumber = request.querystring("FaxNumber")
dim SecondaryEmail = request.querystring("SecondaryEmail")
dim NewAccountNumber = request.querystring("NewAccountNumber")
dim NewAccountPassword = request.querystring("NewAccountPassword")
dim ResellerPlanId = request.querystring("ResellerPlanId")
dim  ResellerAccount = request.querystring("ResellerAccount")
dim  SendWelcomeMessage = request.querystring("SendWelcomeMessage")
dim  Output = request.querystring("Output")

dim xmlDoc
xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.setProperty("ServerHTTPRequest", true)
xmlDoc.load("http://url?FirstName="+FirstName+"&LastName="+LastName+"&PrimaryEmail="+PrimaryEmail+"&Address1="+Address1+"&PostCode="+PostCode+"&CountryCode="+CountryCode+"&Title="+Title+"&CompanyName="+CompanyName+"&Address2="+Address2+"&Address3="+Address3+"&Town="+Town+"&County="+County+"&HomePhone="+HomePhone+"&WorkPhone="+WorkPhone+"&MobilePhone="+MobilePhone+"&FaxNumber="+FaxNumber+"&SecondaryEmail="+SecondaryEmail+"&NewAccountNumber="+NewAccountNumber+"&NewAccountPassword="+NewAccountPassword+"&ResellerPlanId="+ResellerPlanId+"&ResellerAccount="+ResellerAccount+"&SendWelcomeMessage="+SendWelcomeMessage+"&Output="+Output)

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

%>

Edited 6 Years Ago by jamziee: n/a

Hi there thanks for that.

I had to change the DIM FirstName = Request.Querystring("FirstName") to Dim FirstName
FirstName = Request.Querystring("FirstName")

Now I get this error:

Microsoft VBScript compilation error '800a0414'

Cannot use parentheses when calling a Sub

/hosting/data.asp, line 52

xmlDoc.setProperty("ServerHTTPRequest", true)
---------------------------------------------^

Any ideas?

I got this error:

Microsoft VBScript compilation error '800a0401'

Expected end of statement

/hosting/data.asp, line 4

dim FirstName = request.querystring("FirstName")
--------------^

Cheers,
Adam

Here is the code that I have so far..

<%

dim FirstName
FirstName = request.querystring("FirstName")
dim LastName
LastName = request.querystring("LastName")
dim PrimaryEmail
PrimaryEmail = request.querystring("PrimaryEmail")
dim Address1
Address1 = request.querystring("Address1")
dim PostCode
Postcode = request.querystring("PostCode")
dim CountryCode
CountryCode = request.querystring("CountryCode")
dim Title
Title = request.querystring("Title")
dim CompanyName
CompanyName = request.querystring("CompanyName")
dim Address2
Address2 = request.querystring("Address2")
dim Address3
Address3 = request.querystring("Address3")
dim Town
Town = request.querystring("Town")
dim County
County = request.querystring("County")
dim HomePhone
HomePhone = request.querystring("HomePhone")
dim WorkPhone
WorkPhone = request.querystring("WorkPhone")
dim MobilePhone
MobilePhone = request.querystring("MobilePhone")
dim FaxNumber
FaxNumber = request.querystring("FaxNumber")
dim SecondaryEmail
SecondaryEmail = request.querystring("SecondaryEmail")
dim NewAccountNumber
NewAccountNumber = request.querystring("NewAccountNumber")
dim NewAccountPassword
NewAccountPassword = request.querystring("NewAccountPassword")
dim Username
Username = request.querystring("Username")
dim  Password
Password = request.querystring("Password")
dim  SendWelcomeMessage
SendWelcomeMessage = request.querystring("SendWelcomeMessage")

dim xmlDoc
xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.setProperty("ServerHTTPRequest")
xmlDoc.load("http://cp.silverlinewebsolutions.co.uk/billing_api.asp?action="+AddUser+"&FirstName="+FirstName+"&LastName="+LastName+"&PrimaryEmail="+PrimaryEmail+"&Address1="+Address1+"&PostCode="+PostCode+"&CountryCode="+CountryCode+"&Title="+Title+"&CompanyName="+CompanyName+"&Address2="+Address2+"&Address3="+Address3+"&Town="+Town+"&County="+County+"&HomePhone="+HomePhone+"&WorkPhone="+WorkPhone+"&MobilePhone="+MobilePhone+"&FaxNumber="+FaxNumber+"&SecondaryEmail="+SecondaryEmail+"&NewAccountNumber="+NewAccountNumber+"&NewAccountPassword="+NewAccountPassword+"&Username="+Username+"&Password="+Password+"&SendWelcomeMessage="+SendWelcomeMessage)
 
dim request
request = xmlDoc.SelectNodes("//Results")
response.write(request(0).selectSingleNode("ResultCode").text)
response.write("<br>")
response.write(request(0).selectSingleNode("ResultDescription").text)
response.write("<br>")
response.write(request(0).selectSingleNode("ResultData").getAttribute("recordcount"))

And here is the error I am now getting...

Microsoft VBScript runtime error '800a01a8'

Object required: ''

/hosting/data.asp, line 5

Edited 6 Years Ago by spence89: n/a

when you tested it and posted the form did you put any thing in the last name part of the form?

This article has been dead for over six months. Start a new discussion instead.