1

I have this xml

<zone name="main">
        <card number="4" price="0" name="Urza's Tower"/>
        <card number="4" price="0" name="Urza's Power Plant"/>
        <card number="4" price="0" name="Urza's Mine"/>
        <card number="4" price="0" name="Urza's Avenger"/>
        <card number="1" price="0" name="Urza's Miter"/>
        <card number="4" price="0" name="Howling Mine"/>

And I'd like to read the name attributes. I try this but it doesn't work:

 var cards = xmlDoc.getElementsByTagName("zone")[0].childNodes;
      for (var i = 0; i < cards.length; i++) {
        console.log(cards[i].getAttribute("name"));
      }

When I view cards[i] in the debugger it shows all the attributes. I just can't seem to access them...

Thanks!

1
  • try changing the var cards = ... to var cards = xmlDoc.getElementsByTagName("card");. Commented Dec 1, 2016 at 2:28

1 Answer 1

4

The code in the question is using .childNodes so it’s not getting just the card element nodes but also the text nodes between the card elements.

To get just the card element nodes, use .children instead, like this:

 var cards = xmlDoc.getElementsByTagName("zone")[0].children;
      for (var i = 0; i < cards.length; i++) {
        console.log(cards[i].getAttribute("name"));
      }
Sign up to request clarification or add additional context in comments.

Comments

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.