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
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Author Comment

by:Andrew Angell
ID: 16946416
ok hold on...
0
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in query expression 3 54
Writing comments on <p></P> or paragraph 2 23
Obtaining data on ASP 8 36
Asp in server side with Mssql Server 7 4 33
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

749 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