Parse XML In JavaScript

Hey Guys,

I’m having a little trouble getting the JavaScript Parsing of XML to work… I know it’s just my lack of knowledge… :oops: can someone point me in the right direction?

Here’s a snippet:

var rawXML = "<collection><book><title>Cool Book</title><author>Bob</author></book><book><title>Book 2</title><author>John Doe</author></book></collection>";
var xmlObj = new XML(rawXML);
var book1 = xmlObj.collection[0];
var book2 = xmlObj.collection[1];
var title1 = book1.title;
var title2 = book2.title;

Obviously that’s the wrong syntax… ideas?

THANKS in advance!

This should work:

var title1 = xmlObj.book[0].title;
var title2 = xmlObj.book[1].title;

xmlObj is the collection node already, therefor you can go straight to book[0] from xmlObject

Patrick and Paul - thank you!

BTW: In the debugger when you’re in the Interactive Console - and try entering the code - it doesn’t work. When you set real variables - it does… :D