miércoles, 19 de mayo de 2010

jQuery: Leer XML

$.ajax({
url: "programas.xml",
dataType: "xml",
success: function(xmlResponse) {

var data = $("programa", xmlResponse).map(function() {
return {
value: $("name", this).text(),
url: $("url", this).text()
};
}).get();

//data viene ordenada

//alert(data);

$(".search1").autocomplete({
source: data,
minLength: 0,
select: function(event, ui) {
//log(ui.item ? ("Selected: " + ui.item.value + ", geonameId: " + ui.item.id) : "Nothing selected, input was " + this.value);
alert(ui.item.url);
//window.location = ui.item.url;
}
});

}
});

El XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<geonames style="MEDIUM">
<totalResultsCount>6987</totalResultsCount>

<programa>
<name>India</name>
<url>india.html</url>
</programa>

<programa>
<name>Cuna de Lobos</name>
<url>cunadelobos.html</url>
</programa>

<programa>
<name>Detras del Saber</name>
<url>detrasdelsaber.html</url>
</programa>


</geonames>

No hay comentarios:

Publicar un comentario