Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to getElement xml by attribute?

Tags:

javascript

xml

I'm trying to grab an xml node by its attribute.

edit

I'm trying to retrieve an element by its attribute value using javascript xml instead of jquery. Is there a simple method for this?

like image 577
Wenn Avatar asked Aug 16 '11 18:08

Wenn


1 Answers

It is really easy using jQuery. Suppose we have a string like this.

<document>
   <value type="people">
      <name>
         John
      </name>
   </value>
   <value type="vehicle">
      <name>
         Ford
      </name>
   </value>
</document>

Then

var xmlDocument = $.parseXML(str);
$(xmlDocument).find("value[type='people'] name").text()

We will get string 'John' back.

like image 110
Morio Avatar answered Oct 10 '22 18:10

Morio