使用JQuery / Javascript从简单XML获取属性值

时间:2022-04-15 09:03:18

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

我想使用我的javascript从以下简单的xml中获取属性值。

XML :

XML:

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

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

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

The JQuery is working fine if the xml code is

如果xml代码是,JQuery工作正常

<ParentNode Symbol="$"><Row book = "test" price ="80"/>   </ParentNode>

2 个解决方案

#1


11  

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


1  

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

Try this

尝试这个

#1


11  

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


1  

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

Try this

尝试这个