Mick
Mick

Reputation: 2898

Jquery XML results not as expected

I have a jquery script that searches a php file that produces an XML file. The XML file example is here

            <sites>
            <name>Ascot Racecourse</name>
            <address1>Berkshire</address1>
            <postcode>SL5 7JX</postcode>
            <name>Aston Villa Football Club</name>
            <address1>Villa Park</address1>
            <postcode>B6 6HE</postcode>
            <name>Asda 1 Year Celebration</name>
            <address1>Park In Ipswich</address1>
            <postcode>IP</postcode>
            <name>ASDA FULWOOD</name>
            <address1>ASDA FULWOOD</address1>
            <postcode>ASDA</postcode>
            <name>ASDA TEST 5</name>
            <address1>ASDA TEST 5</address1>
            <postcode>ASDA TES</postcode>
            <name>ASDA 50</name>
            <address1>ASDA 50</address1>
            <postcode>ASDA 50</postcode>
            </sites>

My JQ script works fine and gets all the data using the following

          $(document).ready(function(){ 
          $.ajax({
          type: "GET", 
          url: "search_action.php" + string ,
          dataType: "xml",
          success: disxml                                                  });
            })

       } // function 

      function disxml(data){

      $(data).find('sites').each(function(row) {  
                  var name    = $(this).find('name').text(); 
                  var add1    = $(this).find('address1').text(); 

                  display +=   "<b>" + name + "</b> - " + add1  + "<br>"   ;


                  })

      divbox.html(display) ; // draw contents 
      } 

I am happy from checking the XML file in a browser that the result is fine BUT the problem is the value of the variable 'display' I am expecting an ordered list with a line break after each name and address in the xml file, but what I get is ALL the names followed by ALL the addresses. I believe the script is working through a loop and should do what I want but it just doesn't

if it helps this is the value of 'display'

Ascot RacecourseAston Villa Football ClubAsda 1 Year CelebrationASDA FULWOODASDA TEST 5ASDA 50-BerkshireVilla ParkPark In IpswichASDA FULWOODASDA TEST 5ASDA 50

Can anyone tell me what I am doing wrong please ?

Thanks in advance

Mick

Upvotes: 0

Views: 75

Answers (1)

Chad
Chad

Reputation: 19609

This is becasue you are doing the following:

$(data).find('sites').each(function(row) {  
   var name    = $(this).find('name').text(); 
   var add1    = $(this).find('address1').text(); 

   display +=   "<b>" + name + "</b> - " + add1  + "<br>"   ;
})

You are cycling through each sites node, which there is one of, and getting ALL the name nodes within it, and then getting the text of ALL those nodes. For it to work like you are expecting you will have to modify your XML like:

<sites>
   <site>
      <name>Ascot Racecourse</name>
            <address1>Berkshire</address1>
            <postcode>SL5 7JX</postcode>
   </site>
   <site>
      <name>Aston Villa Football Club</name>
            <address1>Villa Park</address1>
            <postcode>B6 6HE</postcode>
   </site>
   <!-- etc... -->
</sites>

And do the following:

$(data).find('sites').find('site').each(function(row) {  
   var name    = $(this).find('name').text(); 
   var add1    = $(this).find('address1').text(); 

   display +=   "<b>" + name + "</b> - " + add1  + "<br>"   ;
})

Upvotes: 3

Related Questions