zero
zero

Reputation: 3084

retrieve attributes from a nodes child node

I'm experimenting with this xml:

<theFeed>
 <games>
    <game id="103"  period="" clock="">
        <team id="657" type="home" logo="1/12"  score="46"/>
        <team id="740" type="visitor"  seed="11" score="59"/>
    </game>
  </games>
</theFeed>

and I'm trying to get the attribute "score" from the first child of the game node, but when I use this code(javascript):

var Hlogo = theXml.getElementsByTagName('game')[0].childNodes[0].getAttribute('score');

it crashes. I can get attributes from the parent just fine using getAttributes... is there something I'm doing wrong?

Upvotes: 6

Views: 17614

Answers (2)

Brad Falk
Brad Falk

Reputation: 196

I believe you need to add a reference to documentElement in your path:

var Hlogo = theXml.documentElement.getElementsByTagName('game')[0].childNodes[0].getAttribute('score');

Upvotes: 1

ckozl
ckozl

Reputation: 6761

var game = theXml.getElementsByTagName('game')[0];
var team = game.getElementsByTagName('team')[0];
var score = team.getAttribute('score');

console.log(game, team, score);

seems to work fine, providing theXml is valid (which i forced it to be document)

hope this helps -ck

Upvotes: 6

Related Questions