Sunday, November 27, 2005

CFMX and Amazon web services, pt 3

Now that we've examined the data structure for a product, we need to determine how many items there are in the dataset and insert them into a variable.

The first order of business to determine how many results are in the xml packet. The following formula will create a variable, numItems, containing how many products there are in the Items node.

<cfset numItems = ArrayLen(tmp.itemsearchresponse.items.xmlchildren)-3>

You may be asking "Why is he deducting three?" Well, as noted in the previous article, the Item child node contains three additional items: Request, TotalPages and TotalResults. These count against the number of results and must be excluded.

You may also ask "Why not just set it to 10?" In this case, there's no telling how many items are in the results. This method prevents smaller resultsets from generating undefined item errors if there is a loop that is preset to ten.

As you begin designing your own Amazon store, you must keep in mind that not all products will have the exact same structure. Product A will have Editorial Review content while Product B won't. Also, Product C will have an EAN while Product D won't. It's because of these incongruities in the xml packets that you must use error-trapping logic to prevent your web page from generating errors.

The first error check is fairly simple. At the beginning of the Items node is a Request subnode. This node, in turn, contains an 'IsValid' child node containing a True/False response relaying whether or not the search encountered any errors during processing.

<cfset chkRequest = tmp.ItemSearchResponse.Items[1].Request.IsValid.xmltext>


One note about the xml packet structure. The format for ItemSearch is ItemSearchResponse.Items.xmlchildren. There can be up to 10 ItemSearchResponse.Items.Item childnodes, but there will only be one ItemSearchResponse.Items node. Thus, when looping through the Items node, it will always be referred to as ItemSearchResponse.Items[1].xmlchild.xmltext.

We can now begin error trapping and assigning values to variables.


<cfset ItemList = ArrayNew(1)>

<cfif chkRequest>

<cfloop from="1" to="#numItems#" index="i">

<cfset ItemList[i] = StructNew()>

  <cfif StructKeyExists(tmp.ItemSearchResponse.Items[1].Item[i],"ASIN")>

    <cfset ItemList[i].ASIN = tmp.ItemSearchResponse.Items[1].Item[i].ASIN.xmltext>

</cfloop>

  </cfif>


As you can see in the above example, the first line creates an single dimension array named ItemList. Then, if chkResult is true, the next level of error-trapping begins. A <cfloop> is started which cycles through the number of products in the xml packet. With each iteration, the ItemList array is converted to a structured array.

A <cfif> contains a StructKeyExists function to determine if the product has an ASIN. If the item has an ASIN, then ItemList[i].ASIN is created and the ASIN.xmltext is added to it.

All Amazon listed products have the same mandatory fields, like Item.ASIN and ItemAttributes.Title. Other fields, such as LowestNewPrice and EAN, are not. In some cases, one or more of these fields will not be defined in xml packet. The StructKeyExists function should, as a best practice, be run for each variable that is to be outputted. It will create additional programming needs but will minimize any potential data errors.

Next up, putting the pieces together and creating a simple input/output form.

Manana,

Chris

No comments: