Solved

How to get around errors while parsing XML when the node isn't returned in a particular response..???

Posted on 2006-06-20
8
267 Views
Last Modified: 2012-05-11
XML Sample: http://www.dealsdirectinc.com/eBayApps/auctionTools/eBayResponses/GetSellerTransactionsResponse.xml

I'm using Microsoft.XMLDOM to parse out data like so:

Set TransactionInfo = xmlDoc.selectNodes("/GetSellerTransactionsResponse/TransactionArray/Transaction")
                  For Each node In TransactionInfo
                        AmountPaid = node.selectSingleNode("AmountPaid").text
                        BuyerEmail = node.selectSingleNode("Buyer/Email").text
                        BuyerFeedbackScore = node.selectSingleNode("Buyer/FeedbackScore").text
                        BuyerStatus = node.selectSingleNode("Buyer/Status").text
                        BuyerEbayID = node.selectSingleNode("Buyer/UserID").text
                        BuyerName = node.selectSingleNode("Buyer/BuyerInfo/ShippingAddress/Name").text
                        BuyerStreet1 = node.selectSingleNode("Buyer/BuyerInfo/ShippingAddress/Street1").text
                        BuyerStreet2 = node.selectSingleNode("Buyer/BuyerInfo/ShippingAddress/Street2").text
                        BuyerCityName = node.selectSingleNode("Buyer/BuyerInfo/ShippingAddress/CityName").text
                        BuyerStateOrProvince = node.selectSingleNode("Buyer/BuyerInfo/ShippingAddress/StateOrProvince").text
                        BuyerCountry = node.selectSingleNode("Buyer/BuyerInfo/ShippingAddress/CountryName").text
                        BuyerPhoneNumber = node.selectSingleNode("Buyer/BuyerInfo/ShippingAddress/Phone").text
                        eBayItemID = node.selectSingleNode("Item/ItemID").text
                        ListingStatus = node.selectSingleNode("Item/SellingStatus/ListingStatus").text
                        FinalValueFee = node.selectSingleNOde("FinalValueFee").text
                  Next
            Set Transactions = Nothing

This works great except for when a particular node doesn't get returned that sometimes might.  For instance, I have BuyerStreet2 being set to the Street2 element.  Well, that element is only returned if it exists, otherwise it doesn't come back at all.  So, given the XML sample above you can see that Street2 does not exist.  Therefore, I end up with the following error:

Microsoft VBScript runtime (0x800A01A8)
Object required: 'node.selectSingleNode(...)'
/dealsdirect/eBayApps/auctionTools/getSellerTransactions.asp, line 162

Line 162, of course, is the line that sets BuyerStreet2.  How do I avoid this?  Any info would be greatly appreciated.  Thanks!

0
Comment
Question by:Andrew Angell
  • 5
  • 3
8 Comments
 
LVL 14

Accepted Solution

by:
CyrexCore2k earned 500 total points
ID: 16946174
Do this

<%
Function TryNode(Node, Loc)
         On Error Resume Next
         TryNode = ""
         TryNode = Node.selectSingleNode("Loc").Text
End Function

Set TransactionInfo = xmlDoc.selectNodes("/GetSellerTransactionsResponse/TransactionArray/Transaction")
               For Each node In TransactionInfo
                    AmountPaid = tryNode(node, "AmountPaid")
                    BuyerEmail = tryNode(node, "Buyer/Email")
                    BuyerFeedbackScore = tryNode(node, "Buyer/FeedbackScore")
                    BuyerStatus = tryNode(node, "Buyer/Status")
                    BuyerEbayID = tryNode(node, "Buyer/UserID")
                    BuyerName = tryNode(node, "Buyer/BuyerInfo/ShippingAddress/Name")
                    BuyerStreet1 = tryNode(node, "Buyer/BuyerInfo/ShippingAddress/Street1")
                    BuyerStreet2 = tryNode(node, "Buyer/BuyerInfo/ShippingAddress/Street2")
                    BuyerCityName = tryNode(node, "Buyer/BuyerInfo/ShippingAddress/CityName")
                    BuyerStateOrProvince = tryNode(node, "Buyer/BuyerInfo/ShippingAddress/StateOrProvince")
                    BuyerCountry = tryNode(node, "Buyer/BuyerInfo/ShippingAddress/CountryName")
                    BuyerPhoneNumber = tryNode(node, "Buyer/BuyerInfo/ShippingAddress/Phone")
                    eBayItemID = tryNode(node, "Item/ItemID")
                    ListingStatus = tryNode(node, "Item/SellingStatus/ListingStatus")
                    FinalValueFee = tryNode(node, "FinalValueFee")
               Next
          Set Transactions = Nothing
%>

If an error occurs the value for that variable will just be an empty string.
0
 
LVL 14

Expert Comment

by:CyrexCore2k
ID: 16946226
Oh shoot typo

Function TryNode(Node, Loc)
         On Error Resume Next
         TryNode = ""
         TryNode = Node.selectSingleNode(Loc).Text
End Function

:D that's what the function should be.
0
 
LVL 11

Author Comment

by:Andrew Angell
ID: 16946407
When I try that I end with blank values in all of those variables...??
0
 
LVL 11

Author Comment

by:Andrew Angell
ID: 16946416
ok hold on...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Author Comment

by:Andrew Angell
ID: 16946627
thanks!
0
 
LVL 11

Author Comment

by:Andrew Angell
ID: 16948285
Can you explain a little bit about how this works?  It does work...fantastic.  I just haven't ever really understood functions.  This looks like a great example of why I need to get it figured out.  Any explanation would be cool.  Thanks!
0
 
LVL 14

Expert Comment

by:CyrexCore2k
ID: 16948913
Sure.

First and foremost is the function itself. Named tryNode obviously for it's function. The definition explains that this function will be expecting two parameters. The first parameter obviously is your node the second parameter is the name or location of the node you're looking for.

So why did I use a function?

Basically one of the major issues with ASP is that it does not have what are termed as "try-catch-blocks" basically in programming languages like .Net the block has two sections, a try section and a catch section. If an error occurs in the try section the code will jump down to the catch section of the block. In your case it would just set the variable to an empty string.

The function I created for you gets around this by using the "On Error Resume Next" statement that is local to the function. Basically if any errors occur in the code within the function that line of code gets ignored and the interpreter moves on. As you can see by looking at the code the return value of the function is set to the empty string (though this is unnecessary since ASP does it for you automatically) and then it "tries" to get the value of the node. If an error occurs like you experienced in your posted script the line is skipped and the return value remains the same empty string.
0
 
LVL 11

Author Comment

by:Andrew Angell
ID: 16954992
ok, cool.  thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Project Bridge error 5 70
currencylayer API intergrate to ASP 25 65
Attaching a file to SMTP in Classic ASP 1 34
classic asp checkbox uncheck and check 2 58
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now