Domanda

Voglio usare il seguente servizio web xml. www.musicbrainz.org/ws/2/artist/?query= artista: michael jackson

quale formato è come di seguito:

<metadata><artist-list offset="0" count="3418"><artist ext:score="100" type="Person" id="f27ec8db-af05-4f36-916e-3d57f91ecf5e"><name>Michael Jackson</name><sort-name>Jackson, Michael</sort-name><gender>male</gender><country>US</country>

Voglio solo analizzare questo xml e ricavarne il sesso. Ho usato il seguente codice per analizzare xml. Qui ottengo l'attributo ext dell'artista ma non funziona.

    $.ajax({
        type: 'GET',
        url: 'http://www.musicbrainz.org/ws/2/artist/?query=artist:michael jackson',
        dataType: 'xml',
        success: function(xml){
           // console.log(xml);
            $(xml).find('artist-list').each(function(){
            $(this).find('artist').each(function(){
                            var ext = $(this).attr('ext');
                            alert(ext);
                    });
            });
    }

});

Chiunque può suggerirmi l'esempio per analizzare xml utilizzando Javascript o jQuery.

È stato utile?

Soluzione

$.ajax({ 
    type: 'GET', 
    url: 'http://www.musicbrainz.org/ws/2/artist/?query=artist:michael jackson',
    dataType: 'xml', 
    success: function(xml){ 
       $("artist", xml).each(function(){
           console.log($("gender", this).text());
       });
    }
});

<⇨Update:

Ho appena controllato il servizio web e ho visto che non tutti i artist hanno un tag gender specificato.In questo caso puoi usare quanto segue:

    $("artist", xml).each(function(){
         var gender = $("gender", this);
         if(gender.length>0)
             console.log($(gender).text());
    });

Guarda la demo di JSFiddle qui

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top