Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
274 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
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/…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 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