Get Attribute Value From Simple XML Using JQuery /

2020-08-27 03:16发布

问题:

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>

回答1:

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



回答2:

$(xml).find("ParentNode").attr("Symbol");

Try this



标签: jquery xml