7 Replies - 1560 Views - Last Post: 20 April 2012 - 04:54 PM

#1 daarkfall   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 27-August 09

XML, AJAX and jQuery

Posted 20 April 2012 - 02:06 AM

Hello D.I.C

I am currently working on a website for a clothing store, and am having huge difficulties when it comes to formatting the output i am getting from an XML String.

The way it works (so far) is i make a call to the controller.php class provided by another developer (this has been tested and works superbly, i then format the XML using this code:

<script type= "text/javascript">
                $(document).ready(function(){
                
                    $.ajax({
                        type: "GET",
                        url: "test1.xml",
                        dataType: "xml",
                        success: function(xml){
                        
                            $(xml).find('ITEM').each(function(){
                                //stock confirmed
                                
                                    //Name confirmed
                                    
                                        //confirmed item
                                        var name = $(xml).find('NAME').text();
                                        var description = $(xml).find('DESCRIPTION').text();
                                        var id = $(xml).find('ID').text();
                                        var price = $(xml).find('PRICE').text();
                                        var imagepath = $(xml).find('IMAGE').text();
										
                                        $(xml).find('SIZES').each(function(){
                                            $(xml).find('SIZE').each(function(){
                                                var symbol = $(xml).find('SYMBOL').text();
                                                var quantity = $(xml).find('QTY').text();
                                            });
                                            
                                        });
										$('<tr></tr>').html('<th>'+name+'</th><td>$'+description+'</td><td>$'+price+'</td>').appendTo('#chart');
                            });
                        }
                    });
                });
            </script>



and i get this output.
T-ShirtBench T-ShirtGrey T-ShirtSonic T-ShirtJeansBlack JeansBlue JeansJacketLeather JacketDenim JacketHatDenim HatJumperPlain JumperAdidas Jumper	$A bench t-shirt thats black!Plain gre T-ShirtT-Shirt with Sonic DesignPair of black jeansPair of blue jeansBlack Leather JacketBlue Denim JacketBlue Denim Design Cowboy HatPlain Blue JumperStriped Adidas Jumper	$12.994.9914.9934.9934.9984.9949.9919.9914.9924.99



My issue is that each of those is an individual item and it returns everything in blocks, here is a sample of my XML string.
<stock>
<CATEGORY>
<NAME>T-Shirt</NAME>
<ITEM>
<NAME>Bench T-Shirt</NAME>
<DESCRIPTION>A bench t-shirt thats black!</DESCRIPTION>
<ID>1</ID>
<PRICE>12.99</PRICE>
<IMAGE>benchtshirt.jpg</IMAGE>
<SIZES>
<SIZE>
<SYMBOL>L</SYMBOL>
<QTY>30</QTY>
</SIZE>
<SIZE>
<SYMBOL>M</SYMBOL>
<QTY>35</QTY>
</SIZE>
<SIZE>
<SYMBOL>S</SYMBOL>
<QTY>18</QTY>
</SIZE>
</SIZES>
</ITEM>
<ITEM>
<NAME>Grey T-Shirt</NAME>
<DESCRIPTION>Plain gre T-Shirt</DESCRIPTION>
<ID>4</ID>
<PRICE>4.99</PRICE>
<IMAGE>grey-tshirt.jpg</IMAGE>
<SIZES>
<SIZE>
<SYMBOL>L</SYMBOL>
<QTY>1</QTY>
</SIZE>
<SIZE>
<SYMBOL>M</SYMBOL>
<QTY>23</QTY>
</SIZE>
<SIZE>
<SYMBOL>S</SYMBOL>
<QTY>1</QTY>
</SIZE>
</SIZES>
</ITEM>
</CATEGORY>
</stock>



I know this question must get asked a million times a day, however i have googled for solutions. I have tried adding each item as i get it in the loop this caused it to over fill the box by about 300 copies of the data.

Any help, pointers, pseudocode, general encouragement would be sincerely appreciated.

Sincerely ~Daarkfall

p.s. test1.xml is a fuller version of the xml provided i am working on it locally and don't want to set up LAMP so for testing sake i am using that.

Is This A Good Question/Topic? 0
  • +

Replies To: XML, AJAX and jQuery

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: XML, AJAX and jQuery

Posted 20 April 2012 - 02:25 AM

the problem is that you try to "find" the sub elements of <ITEM> from the whole XML (and not from the ITEM node). hence you get a jQuery object containing all sub elements (of the whole XML) and text() will get you the combined text of that.
Was This Post Helpful? 1
  • +
  • -

#3 daarkfall   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 27-August 09

Re: XML, AJAX and jQuery

Posted 20 April 2012 - 02:31 AM

thanks you for your response would i be looking to do something like

$(xml).find('NAME').each(function(){
var item = $(xml).find('ITEM').text();
});



so looked for each individual name of items and displaying the entire subtext of the item?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: XML, AJAX and jQuery

Posted 20 April 2012 - 02:35 AM

same problem as above. while you’re inside the each() loop, you still fetch all data from the XML.

$(xml).find("item").each(function(){
    // this = current <item>
    var name = $(this).find("name").text();
    // …
});

This post has been edited by Dormilich: 20 April 2012 - 02:35 AM

Was This Post Helpful? 1
  • +
  • -

#5 daarkfall   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 27-August 09

Re: XML, AJAX and jQuery

Posted 20 April 2012 - 02:52 AM

you are actually an angel in disguise aren't you.

Thank you so much worked a treat.
~Daarkfall
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: XML, AJAX and jQuery

Posted 20 April 2012 - 02:55 AM

well, it depends on the setting. I’m also an Elf, a Viking and a Dwarf. though sometimes I disguise myself as HAL 9000.
Was This Post Helpful? 1
  • +
  • -

#7 BobRodes   User is offline

  • Product Manager
  • member icon

Reputation: 607
  • View blog
  • Posts: 3,086
  • Joined: 19-May 09

Re: XML, AJAX and jQuery

Posted 20 April 2012 - 02:06 PM

Ever notice what happens when you add one to the ascii values of HAL? :)
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: XML, AJAX and jQuery

Posted 20 April 2012 - 04:54 PM

yes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1