1

I need to read an xml node in javascript, just if a certain attr has a specific value. Is there any way without using a loop? I mean something like:

x = xml.getElementsByTagName("someXmlTag")[someAttr=someValue];
3
  • does your environment support xpath? (i.e, is there an evaluate method on xml ... if this is in firefox at least and xml is an xml document, it seems you could use xml.querySelector('someXmlTag[someAttr=someValue]'); Commented Aug 12, 2015 at 14:28
  • I think so. I'm trying something like var matches = xml.querySelectorAll('Tracking[event=viewable_impression]') but still no results. This returns a nodelist object, and I need the value. Commented Aug 12, 2015 at 14:47
  • iterate through the nodelist ... if you only want the FIRST match (or there is only one) see the answer vvvv below - i.e. use querySelector not querySelectorAll Commented Aug 12, 2015 at 14:52

1 Answer 1

2

This works in firefox

var x = xml.querySelector('someXmlTag[someAttr=someValue]');

YMMV with other browsers

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.