I am using classic ASP to parse SOAP XML notifications sent from the eBay api. I am POSTing myself a test example SOAP message from the api documentation. I am able to receive the string of XML data. When I convert the data to string and output xmlString into a text file, the received XML is in there.
When I loadXML(xmlString) into the DOMDocument object and try to output it, all I get is 3 blank lines. Im trying to loop through the elements and parse them, but it appears to be blank inside the DOM object, as I am getting Object Required errors on the lines looking for nodes.
Here is my code, along with the XML message I am POSTing (using Postmaster app) A little guidance would be appreciated!
<%
Function BytesToStr(bytes)
Dim Stream
Set Stream = Server.CreateObject("Adodb.Stream")
Stream.Type = 1 'adTypeBinary
Stream.Open
Stream.Write bytes
Stream.Position = 0
Stream.Type = 2 'adTypeText
Stream.Charset = "iso-8859-1"
BytesToStr = Stream.ReadText
Stream.Close
Set Stream = Nothing
End Function
Dim isPost: isPost = (UCase(Request.ServerVariables("REQUEST_METHOD") & "") = "POST")
'Is it a HTTP POST?
If isPost Then
If Request.TotalBytes > 0 Then
Dim lngBytesCount, xmlString
lngBytesCount = Request.TotalBytes
xmlString = BytesToStr(Request.BinaryRead(lngBytesCount))
Dim xdoc
set xdoc = CreateObject("Msxml2.DOMDocument.6.0")
xdoc.async = false
xdoc.loadXML(xmlString)
call xdoc.setProperty("SelectionNamespaces", "xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='urn:ebay:apis:eBLBaseComponents'")
'**I commented this next part out because I was getting Error:Object Requred
'**I couldnt get any XML from xdoc to print at all
'**see below: WriteLine(xdoc.xml)
'Dim node
'For Each node in xdoc.selectSingleNode("/soapenv:Body/GetItemResponse").ChildNodes
' sResult = sResult & node.xml
'Next
dim fs, tfile
set fs=Server.CreateObject("Scripting.FileSystemObject")
set tfile=fs.OpenTextFile("C:inetpubwwwrootASPtest_notification3.txt", 8, True)
tfile.WriteLine(xdoc.xml)
tfile.WriteLine("")
tfile.Close
set tfile=nothing
set fs=nothing
End If
'Let sender know we have received and processing the message.
Response.Status = "200 OK"
Else
'Return method not allowed
Response.Status = "405 Method Not Allowed"
End If
Response.End
%>
Sample SOAP message posted (from postman app):
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://
www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<ebl:RequesterCredentials soapenv:mustUnderstand="0" xmlns:ns="urn:ebay:apis:eBLBaseComponents"
xmlns:ebl="urn:ebay:apis:eBLBaseComponents">
<ebl:NotificationSignature xmlns:ebl="urn:ebay:apis:eBLBaseComponents">Z2yhKdKmS0Ga5VPmLDOAlg==</
ebl:NotificationSignature>
</ebl:RequesterCredentials>
</soapenv:Header>
<soapenv:Body>
<GetItemResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2008-02-13T03:47:28.106Z</Timestamp>
<Ack>Success</Ack>
<CorrelationID>137541140</CorrelationID>
<Version>553</Version>
<Build>e553_core_Bundled_6057805_R1</Build>
<NotificationEventName>ItemSold</NotificationEventName>
<RecipientUserID>Seller1</RecipientUserID>
<Item>
<AutoPay>false</AutoPay>
<BuyerProtection>ItemEligible</BuyerProtection>
<BuyItNowPrice currencyID="USD">10.0</BuyItNowPrice>
<Country>US</Country>
<Currency>USD</Currency>
<GiftIcon>0</GiftIcon>
<HitCounter>NoHitCounter</HitCounter>
<ItemID>250000627102</ItemID>
<ListingDetails>
<Adult>false</Adult>
<BindingAuction>false</BindingAuction>
<CheckoutEnabled>true</CheckoutEnabled>
<ConvertedBuyItNowPrice currencyID="USD">10.0</ConvertedBuyItNowPrice>
<ConvertedStartPrice currencyID="USD">1.0</ConvertedStartPrice>
<ConvertedReservePrice currencyID="USD">0.0</ConvertedReservePrice>
<HasReservePrice>false</HasReservePrice>
<StartTime>2008-02-13T03:42:45.000Z</StartTime>
<EndTime>2008-02-13T03:43:32.000Z</EndTime>
<ViewItemURL>http://cgi.qa-api012.qa.ebay.com/ws/eBayISAPI.dll?ViewItem&item=250000627102&category=1463</ViewItemURL>
<HasUnansweredQuestions>false</HasUnansweredQuestions>
<HasPublicMessages>false</HasPublicMessages>
<ExpressListing>false</ExpressListing>
<ViewItemURLForNaturalSearch>http://cgi.qa-api012.qa.ebay.com/Test-Item-Won-Sold-notify_W0QQitemZ250000627102QQcategoryZ1463QQcmdZViewItem</ViewItemURLForNaturalSearch>
</ListingDetails>
<ListingDuration>Days_5</ListingDuration>
<ListingType>Chinese</ListingType>
<Location>San Jose, CA</Location>
<PaymentMethods>PaymentSeeDescription</PaymentMethods>
<PaymentMethods>PayPal</PaymentMethods>
<PayPalEmailAddress>[email protected]</PayPalEmailAddress>
<PrimaryCategory>
<CategoryID>1463</CategoryID>
<CategoryName>Collectibles:Trading Cards:Phone Cards</CategoryName>
</PrimaryCategory>
<PrivateListing>false</PrivateListing>
<Quantity>1</Quantity>
<ReservePrice currencyID="USD">0.0</ReservePrice>
<ReviseStatus>
<ItemRevised>false</ItemRevised>
</ReviseStatus>
<Seller>
<AboutMePage>false</AboutMePage>
<Email>[email protected]</Email>
<FeedbackScore>100</FeedbackScore>
<PositiveFeedbackPercent>98.1</PositiveFeedbackPercent>
<FeedbackPrivate>false</FeedbackPrivate>
<FeedbackRatingStar>Turquoise</FeedbackRatingStar>
<IDVerified>false</IDVerified>
<eBayGoodStanding>true</eBayGoodStanding>
<NewUser>false</NewUser>
<RegistrationDate>2004-02-23T23:50:13.000Z</RegistrationDate>
<Site>US</Site>
<Status>Confirmed</Status>
<UserID>Seller1</UserID>
<UserIDChanged>false</UserIDChanged>
<UserIDLastChanged>2004-02-23T23:48:54.000Z</UserIDLastChanged>
<VATStatus>NoVATTax</VATStatus>
<SellerInfo>
<AllowPaymentEdit>true</AllowPaymentEdit>
<CheckoutEnabled>true</CheckoutEnabled>
<CIPBankAccountStored>false</CIPBankAccountStored>
<GoodStanding>true</GoodStanding>
<MerchandizingPref>OptIn</MerchandizingPref>
<QualifiesForB2BVAT>false</QualifiesForB2BVAT>
<SellerLevel>None</SellerLevel>
<StoreOwner>false</StoreOwner>
<ExpressEligible>false</ExpressEligible>
<ExpressWallet>false</ExpressWallet>
<SafePaymentExempt>true</SafePaymentExempt>
</SellerInfo>
<MotorsDealer>false</MotorsDealer>
</Seller>
<SellingStatus>
<BidCount>2</BidCount>
<BidIncrement currencyID="USD">0.25</BidIncrement>
<ConvertedCurrentPrice currencyID="USD">1.25</ConvertedCurrentPrice>
<CurrentPrice currencyID="USD">1.25</CurrentPrice>
<HighBidder>
<AboutMePage>false</AboutMePage>
<EIASToken>nY+sHZ2PrBmdj6wVnY+sEZ2PrA2dj6wJkoWoCJaGoAmdj6x9nY+seQ==</EIASToken>
<Email>[email protected]</Email>
<FeedbackScore>5</FeedbackScore>
<PositiveFeedbackPercent>100.0</PositiveFeedbackPercent>
<FeedbackPrivate>false</FeedbackPrivate>
<FeedbackRatingStar>None</FeedbackRatingStar>
<IDVerified>false</IDVerified>
<eBayGoodStanding>true</eBayGoodStanding>
<NewUser>false</NewUser>
<RegistrationDate>2004-02-24T07:00:00.000Z</RegistrationDate>
<Site>US</Site>
<Status>Confirmed</Status>
<UserID>Buyer1</UserID>
<UserIDChanged>false</UserIDChanged>
<UserIDLastChanged>2004-02-24T07:00:00.000Z</UserIDLastChanged>
<VATStatus>NoVATTax</VATStatus>
<BuyerInfo>
<ShippingAddress>
<Country>US</Country>
<PostalCode>95125</PostalCode>
</ShippingAddress>
</BuyerInfo>
<UserAnonymized>false</UserAnonymized>
</HighBidder>
<LeadCount>0</LeadCount>
<MinimumToBid currencyID="USD">1.5</MinimumToBid>
<QuantitySold>1</QuantitySold>
<ReserveMet>true</ReserveMet>
<SecondChanceEligible>true</SecondChanceEligible>
<ListingStatus>Completed</ListingStatus>
</SellingStatus>
<ShippingDetails>
<AllowPaymentEdit>true</AllowPaymentEdit>
<ApplyShippingDiscount>false</ApplyShippingDiscount>
<InsuranceOption>NotOffered</InsuranceOption>
<SalesTax>
<SalesTaxPercent>0.0</SalesTaxPercent>
<ShippingIncludedInTax>false</ShippingIncludedInTax>
</SalesTax>
<ThirdPartyCheckout>false</ThirdPartyCheckout>
<TaxTable/>
</ShippingDetails>
<ShipToLocations>US</ShipToLocations>
<Site>US</Site>
<StartPrice currencyID="USD">1.0</StartPrice>
<TimeLeft>PT0S</TimeLeft>
<Title>Test Item{Won/Sold} notify</Title>
<HitCount>0</HitCount>
<GetItFast>false</GetItFast>
<PostalCode>
</PostalCode>
<PictureDetails>
<PhotoDisplay>None</PhotoDisplay>
</PictureDetails>
<ProxyItem>false</ProxyItem>
</Item>
</GetItemResponse>
</soapenv:Body>
</soapenv:Envelope>
2
Answers
Well after testing your code it isn’t the
Charset
property causing the problem (but I would make sure it is set toUTF-8
anyway).The issue is the XML failing to parse and when checking the
parseError
found this;Then I took a look at the XML you posted and it does have some hard wrapping in there which I removed and tested again, this time it succeeded.
Modified XML (removed hard wrapping)
Here is the modified code to help debug
Should point out that to test the file creation I modified the path but your hard coded path on your system should work just fine. I modified it so that I could use
Server.MapPath()
to map the physical location of a folder I calledfiles
outside of the website and give the anonymous account the required permission to write to it. You don’t have to do any of this, I was just explaining why the code looks different.Parsing XML
Couple of things to point out.
The
loadXML()
method returns a Boolean to tell you whether the XML successfully loaded or not. If the return value isFalse
useparseError
object to retrieve the last error the parser encountered while validating the XML.In the example above I pass it back as a
HTTP 400 Bad Request
response with theerrorCode
andReason
properties as the body of the message.Related
Answer to Receiving SOAP notifications from eBay api into ASP variable?
Accessing a request’s body (Credit to @kul-tigin for the
BytesToStr()
function)Accepted solution is definitely the answer to the question.
But I want to show other methods related with little known capabilities of the MSXML.
This, processing XML request body also can be done in more reliable binary-safe ways without string conversions, possible charset / encoding headaches etc.
load method can handle ASP’s Request object, a Stream object (or an object implements IStream) or SAFEARRAY of unsigned bytes.
from load Method (C/C++ Syntax)
Based on Lankymart’s solution (thanks for the credit BTW), from simple to complex:
Method #1 – The
Request
objectMethod #2 – SAFEARRAY
Method #3 –
Adodb.Stream
object (binary type)Hope it helps.