Forum Moderators: open

Message Too Old, No Replies

Parse XML for package data

         

mudogg80

4:38 pm on Feb 15, 2010 (gmt 0)

10+ Year Member



I have an xml document that I 'm loading with javascript and I need to parse this xml for products and packages based on user selection. The application i'm working on has
products listed on a page that can be added to a shopping cart. If the user selects 3 products, I need to parse the xml for the combination of the those selected product id's
that correspond to an individual package. So in the below example, the user selects products 10, 11, 12 and adds them to the shopping cart. I need to search the xml (with javascript)
to find if a package exists. If the package exists, return the <package> information for that particular <packageid>. Likewise if the user only selects 2 products, search the
xml for a package that exists containing the 2 product id's, and display the package information.

Here is a sample xml doc:
<product>
<id>10</id>
<title>Product 10</title>
</product>

<product>
<id>11</id>
<title>Product 11</title>
</product>

<product>
<id>12</id>
<title>Product 12</title>
</product>

<product>
<id>13</id>
<title>Product 13</title>
</product>

<package>
<packageid>100</packageid>
<id1>10</id1>
<id2>11</id2>
<id3>12</id3>
<price>$65.00</price>
<text>3 package deal</text>
</package>

<package>
<packageid>101</packageid>
<id1>10</id1>
<id2>13</id2>
<price>$35.00</price>
<text>2 package deal</text>
</package>



Any help or guidance would be much appreciated! Thanks in advance!

Silvery

3:34 pm on Feb 20, 2010 (gmt 0)

10+ Year Member



I'm not sure why you wouldn't simply generate the products in a format that could be more easily processed, such as a Javascript array.

I think loading the content into an array would be more efficient than trying to load all the XML, then searching text strings. XML is pretty verbose.