0

I have the following xml file which I'm trying to read with JQuery

<albums startAlbumNo="1">


    <album id="1">

        <author>author1</author>

        <image>images/2010-1-web-ready.jpg</image>

        <caption>caption1</caption>

        <tracks>
            <item id="31">
                <title>title1</title>
                <artist>artist1</artist>
                <song>content/stories/ChristmasStory.mp3</song>
            </item>
            <item id="32">
                <title>title2</title>
                <artist>artist2</artist>
                <song>content/stories/ChristmasStory2.mp3</song>
            </item>
        </tracks>

    </album>

    //similiar albums below
    <album>....</album>
    <album>....</album>
    <album>....</album>

</albums>    

And I have the following JQuery to read it

 $(document).ready(function(){    //run when page loads

var buttonNames = new Array();
var foo;

function parse(document){    //extract song locations from mp3gallery.xml based upon album

  buttonNames[0] = $(document).find('album[id="1"]').find("tracks").find("title").eq(0).text(); //this works

   $(document).find("album").each(function(){  //this does not
     alert('1');  //never runs
     foo = $(this).find('image').text();
    }
  alert(foo);  //never runs


   changeButtonNames(); 
}

  $.ajax({
    url: 'mp3gallery/xml/mp3gallery.xml', // name of file you want to parse
    dataType: "xml",
    success: parse,  //on success calls the "parse" function
    error: function(){alert("Error: Something went wrong");}

  });


  function changeButtonNames(){ //has to be run last

      document.getElementById('btn1').innerHTML = buttonNames[0]; 
  }

});

the

$(document).find('album[id="1"]').find("tracks").find("title").eq(0).text();

runs fine, however my .each loop does nothing.

I have been looking at numerous examples for a while with no success. I am probably missing something obvious. Thanks for your help!

1
  • 1
    I'm not sure what the implications are of passing in a variable named document, maybe try changing that to something like xml Commented Apr 27, 2013 at 19:32

1 Answer 1

2

there is an error in your code

make it

$(document).find("album").each(function(){  //this does not
     alert('1');  //never runs
     foo = $(this).find('image').text();
});
Sign up to request clarification or add additional context in comments.

1 Comment

After hours of looking at it I forgot the closing ) lol. Thanks so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.