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
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
269 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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 …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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…

791 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