Tema: XML y jQuery
Ver Mensaje Individual
  #2 (permalink)  
Antiguo 02/05/2012, 07:56
Avatar de Naahuel
Naahuel
 
Fecha de Ingreso: marzo-2011
Ubicación: localhost
Mensajes: 796
Antigüedad: 13 años, 2 meses
Puntos: 192
Respuesta: XML y jQuery

Revisá la documentación de $.parseXML:
http://api.jquery.com/jQuery.parseXML/

Supongamos un XML como este:
http://vimeo.com/api/v2/brad/appears_in.xml

Y queremos listar los videos con jQuery. Haríamos así:


Código HTML:
Ver original
  1. <!DOCTYPE html>
  2. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  3. <script type="text/javascript">
  4.     $(function(){
  5.         var XML = ""; //obtener aca el XML
  6.        
  7.         var xmlDoc = $.parseXML( XML );
  8.         var $xml = $( xmlDoc );
  9.        
  10.         //Obtener todos los videos
  11.         var $videos = $xml.find('video');
  12.        
  13.         var $cont = $('#videos');
  14.         //listarlos todos
  15.         $videos.each(function(){
  16.             var $elem = $(this);
  17.             var titulo  = $elem.find('title').text();
  18.             var url = $elem.find('url').text();
  19.             var desc = $elem.find('description').text();
  20.             var thumb = $elem.find('thumbnail_small').text();
  21.            
  22.             $cont.append('<li><h2><a href="'+url+'">'+titulo+'</a></h2><p><img src="'+thumb+'" /></p><p>'+desc+'</p></li>');
  23.         });
  24.        
  25.     });
  26.  
  27.  
  28. <title>XML Test</title>
  29. </head>
  30.     <ul id="videos">
  31.        
  32.     </ul>
  33. </body>
  34. </html>

Acá lo podés ver funcionando:
http://jsbin.com/ixulub

Te recomiendo que uses $.ajax en lugar de $.get, de forma que puedas especificar que el tipo de datos que esperas es XML.

Saludos.
__________________
nahueljose.com.ar