Return to Snippet

Revision: 50920
at September 9, 2011 01:17 by alexscript


Updated Code
//Perform when the document is ready/loaded/rendered
$(document).ready(function(){
   //Asynchronously retrieve the xml file contents		
   $.ajax({				
      type: "GET",				
      url: "sites.xml",				
      dataType: "xml",				
      success: function(xml) { //Upon successful retrieval
         //Iterate through the all the nodes/items
         $(xml).find('site').each(function(){ //For each item, perform the following						
            //Read each child node and associate the values with a variable
            var id = $(this).attr('id'); //Find an attribute within the "site" node/element						
            var title = $(this).find('title').text(); //Find the child element of "site" called title						
            var url = $(this).find('url').text(); //Find the child element of "site" called title						
            //Write out a custom link with the values above
            $('<div class="items" id="link_'+id+'"></div>').html('<a href="'+url+'">'+title+'</a>').appendTo('#page-wrap');
            //Establish the "desc" node/element as the this value below, to be referenced fro child nodes/elements
            $(this).find('desc').each(function(){							
               var brief = $(this).find('brief').text();
               var long = $(this).find('long').text();
               $('<div class="brief"></div>').html(brief).appendTo('#link_'+id);
               $('<div class="long"></div>').html(long).appendTo('#link_'+id);
            });					
         });				
      }			
   });		
});

Revision: 50919
at September 9, 2011 01:12 by alexscript


Updated Code
//Perform when the document is ready/loaded/rendered
$(document).ready(function(){
   //Asynchronously retrieve the xml file contents		
   $.ajax({				
      type: "GET",				
      url: "sites.xml",				
      dataType: "xml",				
      success: function(xml) { //Upon successful retrieval
         //Iterate through the all the nodes/items
         $(xml).find('site').each(function(){ //For each item, perform the following						
            //Read each child node and associate the values with a variable
            var id = $(this).attr('id');						
            var title = $(this).find('title').text();						
            var url = $(this).find('url').text();						
            //Write out custom content to the page
            $('<div class="items" id="link_'+id+'"></div>').html('<a href="'+url+'">'+title+'</a>').appendTo('#page-wrap');
            //For each description 
            $(this).find('desc').each(function(){							
               var brief = $(this).find('brief').text();
               var long = $(this).find('long').text();
               $('<div class="brief"></div>').html(brief).appendTo('#link_'+id);
               $('<div class="long"></div>').html(long).appendTo('#link_'+id);
            });					
         });				
      }			
   });		
});

Revision: 50918
at September 9, 2011 01:08 by alexscript


Updated Code
$(document).ready(function(){			
   $.ajax({				
      type: "GET",				
      url: "sites.xml",				
      dataType: "xml",				
      success: function(xml) {
         $(xml).find('site').each(function(){						
            var id = $(this).attr('id');						
            var title = $(this).find('title').text();						
            var url = $(this).find('url').text();						
            $('<div class="items" id="link_'+id+'"></div>').html('<a href="'+url+'">'+title+'</a>').appendTo('#page-wrap');
            $(this).find('desc').each(function(){							
               var brief = $(this).find('brief').text();
               var long = $(this).find('long').text();
               $('<div class="brief"></div>').html(brief).appendTo('#link_'+id);
               $('<div class="long"></div>').html(long).appendTo('#link_'+id);
            });					
         });				
      }			
   });		
});

Revision: 50917
at September 9, 2011 01:05 by alexscript


Updated Code
$(document).ready(function(){			
   $.ajax({				
      type: "GET",				
      url: "sites.xml",				
      dataType: "xml",				
      success: function(xml) {					          $(xml).find('site').each(function(){						
        var id = $(this).attr('id');						
        var title = $(this).find('title').text();						
        var url = $(this).find('url').text();						
        $('<div class="items" id="link_'+id+'"></div>').html('<a href="'+url+'">'+title+'</a>').appendTo('#page-wrap');						           $(this).find('desc').each(function(){							
          var brief = $(this).find('brief').text();							          var long = $(this).find('long').text();							$('<div class="brief"></div>').html(brief).appendTo('#link_'+id);							$('<div class="long"></div>').html(long).appendTo('#link_'+id);						});					
});				
}			
});		
});

Revision: 50916
at September 9, 2011 01:02 by alexscript


Initial Code
$(document).ready(function(){			$.ajax({				type: "GET",				url: "sites.xml",				dataType: "xml",				success: function(xml) {					$(xml).find('site').each(function(){						var id = $(this).attr('id');						var title = $(this).find('title').text();						var url = $(this).find('url').text();						$('<div class="items" id="link_'+id+'"></div>').html('<a href="'+url+'">'+title+'</a>').appendTo('#page-wrap');						$(this).find('desc').each(function(){							var brief = $(this).find('brief').text();							var long = $(this).find('long').text();							$('<div class="brief"></div>').html(brief).appendTo('#link_'+id);							$('<div class="long"></div>').html(long).appendTo('#link_'+id);						});					});				}			});		});

Initial URL
http://think2loud.com/224-reading-xml-with-jquery/

Initial Description


Initial Title
How to read an xml file with jquery/javascript

Initial Tags
javascript, xml, jquery

Initial Language
JavaScript