PDA

View Full Version : ASP: Obtaining Data from Simple XML Response


RobinR
10-01-2004, 04:25 PM
When you receive an XML response back and there might be a node with a particular name but there may not be, how do you handle that? Also, when a node has no child nodes, what is the best way to obtain the data in it?

In the sample response from Fedex, I’d like to:
- get the Meter Number
- get the attribute in SubscribedService
- find out if there is an error if the request is invalid

It should be simple but I’m having difficulty with it. Also, according to my UPS documentation, you should be able to find out if a node exists with the following code. However, I get an “object required” error message when trying to use it:

<%
Set xmlDoc = Server.CreateObject(“MSXML.DOMDocument”)

XmlDoc.validateonParse = False
XmlDoc.loadXML(xmlStr)

If xmlDoc.documentElement.nodeName = “Error” Then

End If

%>

I would like to use the XML DOM to extract the response text.

Thanks for your help,

Robin

*************************************************************
Fedex Response Example 1 (Valid Request):

<?xml version="1.0" encoding="UTF-8"?>

<FDXSubscriptionReply xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FDXSubscriptionReply.xsd">

<RequestHeader>
<CustomererTransactionIdentifier>String</CustomerTransactionIdentifier>
</RequestHeader>

<MeterNumber>1234567</MeterNumber>
<SubscribedService>
<SubscribedService Name="FedEx ExpressShipping" />
</SubscribedService>
</FDXSubscriptionReply>

**********************************************************
Fedex Response Example 2 (Error):

<?xml version="1.0" encoding="UTF-8"?>

<FDXSubscriptionReply xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FDXSubscriptionReply.xsd">

<RequestHeader>
<CustomererTransactionIdentifier>String</CustomerTransactionIdentifier>
</RequestHeader>

<Error>
<Code>20528</Code>
<Message>Invalid Zip Code</Message>
</Error>
</FDXSubscriptionReply>

afterburn
10-01-2004, 06:09 PM
You can check to see if the node is there. By DOMobj.getElementsByTagName("NAME").length > 0

.....

RobinR
10-01-2004, 06:26 PM
Afterburn,

Thanks. Can you help with the other stuff?

In the sample response from Fedex, I’d like to:
- get the Meter Number
- get the attribute in SubscribedService


Robin

afterburn
10-01-2004, 08:26 PM
xmlDom.setProperty "SelectionLanguage", "XPath" 'Sets langauge
xmlDom.validateOnParse = false 'stops DTD errors
xmlDom.async = false 'ASP is not able to do this correctly

those properties are best set.

THen you can query like so..

strName = "RequestHeader/CustomererTransactionIdentifier"
strNameAttribute = "RequestHeader/SubscribedService/SubscribedService/@Name"


For index = 0 to DOMobj.getElementsByTagName(strName).length -1
'Do something with node.
xmlDom.getElementsByTagName(strName).item(Index)
Next


now the XML attributes

xmlDom.getElementsByTagName(strName ).item(NodeIndex).getAttribute(AttribName).Nodetypedvalue




SubscribedService has an error its self containing>?????

Thats bad form, but anyways not completely sure if that is the correct syntax for attributes but should be ...

RobinR
10-09-2004, 05:52 PM
After correcting start tag <CustomererTransactionIdentifier>
to <CustomerTransactionIdentifier>, the following code was used to extract the XML Response.

Robin
*****************

<%
If xmlBodyDocument.getElementsByTagName("MeterNumber").length > 0 Then
Set MeterNumberElement = xmlBodyDocument.getElementsByTagName("MeterNumber").item(0)

strMeterNumber = MeterNumberElement.text
Response.Write "Meter Number: " & strMeterNumber & "<BR>"

Set SubscribedServiceElement =
xmlBodyDocument.getElementsByTagName("SubscribedService").item(0)

For each xmlNode in SubscribedServiceElement.childNodes
If xmlNode.nodeName = "SubscribedService" Then
strSubscribedServiceName = xmlNode.getAttribute("Name")
Response.Write "Subscribed Service: " & strSubscribedServiceName & "<BR>"
End If
Next

Else

If xmlBodyDocument.getElementsByTagName("Error").length > 0 Then
Set ErrorElement = xmlBodyDocument.getElementsByTagName("Error").item(0)

For each xmlNode in ErrorElement.childNodes
If xmlNode.nodeName = "Code" Then
strErrorCode = xmlNode.text
Response.Write "Code: " & strErrorCode & "<BR>"
End If

If xmlNode.nodeName = "Message" Then
strErrorMessage = xmlNode.text
Response.Write "Error Message: " & strErrorMessage & "<BR>"
End If
Next
End If
End If
%>