StackOverFlow
StackOverFlow

Reputation: 4614

How to parse xmlString with jquery without ajax call?

I want to parse xmlString with jquery but without using ajax call ?

xmlString =

 <?xml version="1.0" encoding="UTF-8"?>
        <root>  
            <item id="4" parent_id="0" state="close">  
                <content><name>Charles Madigen</name></content>                
            </item>  
            <item id="192" parent_id="4" state="close">    
          <content><name>Ralph Brogan</name></content>  

            </item> 
         </root>

I want to parse above xml for IDs?
4 , 192.... How to parese xmlString with jquery.

Xml parsing with jquery+ ajax call

In my case I dont have any xml file ex. a.xml

On some operation I got xmlString which I want to parse for ID

Any help or guidance in this matter would be appreciated

Upvotes: 0

Views: 3847

Answers (1)

phantasm
phantasm

Reputation: 572

You should try something like this:

var xmlString = '<xml><some myAttr="1">test</some></xml>';
var xmlDOM = $.parseXML(xmlString);
$(xmlDOM).find('some').attr('myAttr') // yields "1"

In other words, you can treat your XML string as usual DOM structure, and use any jQuery selectors and methods.

As for your example, code will look like this:

var xmlDOM = $.parseXML(xmlString);
var items = $(xmlDOM).find('root item');
$.each (items, function(key, val){
    alert ($(val).attr('id'))
})

Upvotes: 1

Related Questions