January 04, 2006

Parsing XML with JavaScript: anything easy?

Now I'm looking at browser-side libraries to parse XML into a useful data structure, such as:

var doc = '<team name="Steelers" coach="Bill Cowher">' +
          '  <player>' +
          '    <position>Wide receiver</position>' +
          '    <name>Hines Ward</name>' +
          '  </player>' +
          '  <player>' +
          '    <position>Running back</position>' +
          '    <name>Jerome Bettis</name>' +
          '  </player>' +
var parsed = someParseMethod( doc );
is( parsed.name, "Steelers" );
is( parsed.coach, "Bill Cowher" );
is( parsed.player[0].position, "Wide receiver" );
is( parsed.player[0].name, "Hines Ward" );
is( parsed.player[1].position, "Running back" );
is( parsed.player[1].name, "Jerome Bettis" );

And I seem to be coming up short: every library wants to parse an XML document into a W3C DOM or some sort of DOM facsimile. Yuck. Why deal with all those methods when you've got dynamic properties?

I figured this was a gimme since so many people are working with XML on the client these days. (Or are they?) So is there a library out there that does this basic thing?

Next: A possible side-effect of Barb's job change: getting sick
Previous: Notes while researching AJAX libraries