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
266 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

17 Experts available now in Live!

Get 1:1 Help Now