Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Attribute Value From Simple XML Using JQuery / Javascript

Tags:

jquery

xml

I tring to get the attribute value from the following simple xml using my javascript.

XML :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ParentNode Symbol="$"><Row book = "test" price ="80"/></ParentNode>

jQuery: $('ParentNode').attr('Symbol');

The JQuery is working fine if the xml code is

<ParentNode Symbol="$"><Row book = "test" price ="80"/>   </ParentNode>
like image 604
Mahesh Ramasamy Avatar asked Aug 27 '13 12:08

Mahesh Ramasamy


People also ask

How to get value of an attribute in jQuery?

The attr() method sets or returns attributes and values of the selected elements. When this method is used to return the attribute value, it returns the value of the FIRST matched element.

How to retrieve XML node value?

Get the Value of an Attribute Unlike element nodes, attribute nodes have text values. The way to get the value of an attribute, is to get its text value. This can be done using the getAttribute() method or using the nodeValue property of the attribute node.


1 Answers

Try

var string  = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ParentNode Symbol="$"><Row book = "test" price ="80"/></ParentNode>';

var $doc = $.parseXML(string);
console.log($($doc).find('ParentNode').attr('Symbol'))

Demo: Fiddle

like image 104
Arun P Johny Avatar answered Sep 22 '22 09:09

Arun P Johny