Wszystko robię na przykładnie pliku XML z last.fm (http://cdn.last.fm/bestof/2009/bestof2009.xml).
Chciałem wyodrębnić 10 pierwszych nazw i lokacji z tego pliku.. Zanim przeszedłem do tworzenia całego skryptu, utworzyłem nowy plik i zapisałem w nim tylko jednego artyste, czyli struktura wyglądała tak:
<?xml version="1.0" ?> <artist> <position>1</position> <name>Lady GaGa</name> <location>New York, United States</location> <iso>US</iso> <url>http://www.last.fm/music/Lady+GaGa</url> <images> <small>http://userserve-ak.last.fm/serve/34/27576621.png</small> <medium>http://userserve-ak.last.fm/serve/64/27576621.png</medium> <large>http://userserve-ak.last.fm/serve/126/27576621.png</large> </images> <stats> <overall> <plays>35562384</plays> <listeners>955164</listeners> </overall> <year> <listeners>755309</listeners> <gigs>253</gigs> <festivals>54</festivals> <users_attended>15709</users_attended> </year> </stats> <tags> <tag> <name>pop</name> <url>http://www.last.fm/tag/pop</url> </tag> <tag> <name>dance</name> <url>http://www.last.fm/tag/dance</url> </tag> <tag> <name>electronic</name> <url>http://www.last.fm/tag/electronic</url> </tag> </tags> <album> <name>The Fame</name> <url>http://www.last.fm/music/Lady+GaGa/The+Fame</url> <images> <small>http://userserve-ak.last.fm/serve/34s/24408387.png</small> <medium>http://userserve-ak.last.fm/serve/64s/24408387.png</medium> <large>http://userserve-ak.last.fm/serve/126/24408387.png</large> </images> <stats> <overall> <plays>17626729</plays> <listeners>359732</listeners> </overall> <year> <plays>18487195</plays> </year> </stats> <tags> <tag> <name>pop</name> <url>http://www.last.fm/tag/pop</url> </tag> <tag> <name>albums i own</name> <url>http://www.last.fm/tag/albums%20i%20own</url> </tag> <tag> <name>dance</name> <url>http://www.last.fm/tag/dance</url> </tag> </tags> </album> </artist>
Mój kod wygląda naastępująco:
function doAjax() { var myRequest = new XMLHttpRequest; myRequest.open("GET","small.xml",false); myRequest.send(null); response = myRequest.responseXML; if(myRequest.readyState == 4) { if(myRequest.status == 200) { var artist = response.getElementsByTagName("name")[0].firstChild.nodeValue; var location = response.getElementsByTagName("location")[0].firstChild.nodeValue; window.document.getElementById('resultDiv').innerHTML = artist + "-" + location; } } }
dzięki niemu bezproblemowo otrzymuje string "Lady Gaga - New York, United States". Problem zaczyna się gdy do pliku XML dodam kolejnego artyste. Wtedy podmiana elementu tablicy na 1 nie działa, a również wyświetlanie pierwszego elementu [0] nie jest ponownie możliwe.
próbowałem najpierw wybierać wszystkie znaczniki ARTIST i z niego wziąć childNodes[1] dla nazwy wykonawcy, ale również nie działa..
ktoś może potrafi wytknąć mi błędy jakie robię w rozumowaniu tego parsowania, jakiekolwiek wskazówki i naprowadzenie na właściwą drogę sa na wagę złota

Zauważyłem dziwną rzecz. postanowiłem sam stworzyć prosty plik xml, który przedstawia się następująco:
<?xml version="1.0" ?> <artist> <name>Artysta 1</name> <location>Lokalizacja 1</location> </artist> <artist> <name>Artysta 2</name> <location>Lokalizacja 2</location> </artist>
wyświetlanie robię poprzez:
var myRequest = new XMLHttpRequest; myRequest.open("GET","small.xml",false); myRequest.send(null); response = myRequest.responseText; if(myRequest.readyState == 4) { if(myRequest.status == 200) { document.getElementById('writeroot').innerHTML += response; } }
i wyświetla ale tylko wszystko co jest zawarte w pierwszym <artist></artist>, a specjalnie zmieniłem na responseText, aby otrzymać cały plik. Czy to może być coś z konfiguracją serwera, z plikiem? czy coś innego? jestem w kropce, a błąd wydaje się być absurdalny..