0

Is it possible to access an XML section / node using an attribute value (e.g. id) using JavaScript?

For example:

<people>
    <person id='1'>
        <name>Tom</name>
        <age>30</age>
    </person>
    <person id='2'>
        <name>Ian</name>
        <age>22</age>
    </person>
    <person id='3'>
        <name>Ben</name>
        <age>45</age>
    </person>
</people>

I want to be able to select a person with a given id. For example, finding the ID 2 would return Ian.

This is possible in ActionScript like so:

xml.people.person.(@id == 2).name

Is something similar available in JS?

2
  • The same thing works in JavaScript if you have E4X, which only Firefox has. So on the off chance that you're developing a Firefox extension... :) Commented Jun 27, 2012 at 16:07
  • You may also want to look into jQuery.parseXML: api.jquery.com/jQuery.parseXML Commented Jun 27, 2012 at 16:47

2 Answers 2

1

Assuming you have an XMLDocument, the only cross-browser way is to loop through the nodes:

var people = doc.getElementsByTagName('person');

for(var i = 0; i < people.length; i++) {
    if(people[i].getAttribute('id') === '2') {
        // Found it!
    }
}

If you need to do very complicated expressions, however, it may be worth it to use XPath, even though it needs a bit of cross-browser checking.

Here's the standard way:

document.evaluate('//person[@id='2']/text()[0]', doc.documentElement, null, XPathResult.STRING_TYPE, null).stringValue;
Sign up to request clarification or add additional context in comments.

3 Comments

It's a bad way to search for element, especially if the xml document is big. And it's not the only way, browsers do support XPath.
@DonatasOlsevičius: I was in the process of adding XPath to my answer, but it's annoying in Internet Explorer, hence "the only cross-browser way".
w3schools say it works in IE too, you just have to add some more code for it - w3schools.com/xpath/tryit.asp?filename=try_xpath_select_cdnodes However I cannot try it, I'm a Linux user.
1
var xml_text="<people><person id='1'><name>Tom</name><age>30</age></person><person id='2'><name>Ian</name><age>22</age></person><person id='3'><name>Ben</name><age>45</age></person></people>";

var xmlParser = new DOMParser().parseFromString(xml_text, "text/xml").documentElement;

var person2Name = xmlParser.querySelector("people person[id='2'] name").textContent ​​;

querySelector takes as input a string, which acts as a css selector on the node.

textContent gets the text of a node.

IE8 doesnt support DOMParser: but you can use the following workaround

  xmlParser =new ActiveXObject("Microsoft.XMLDOM");
  xmlParser.async=false;
  xmlParser.loadXML(xml_text); 

1 Comment

+1, this is much better than XPath. Annoying Internet Explorer 8 doesn't support either DOMParser or easy XPath, though, even though it does support querySelector...

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.