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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18465940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:41:22  来源:igfitidea点击:

Get Attribute Value From Simple XML Using JQuery / Javascript

jqueryxml

提问by Mahesh Ramasamy

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>

回答by Arun P Johny

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

演示:小提琴

回答by Mahesh Ramasamy

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

Try this

尝试这个