使用Javascript或Jquery解析xml标记属性? [英] Parse xml tag attributes using Javascript or Jquery?

查看:75
本文介绍了使用Javascript或Jquery解析xml标记属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml. XML示例在下面给出

I have one xml. Example XML is given below

<company sample="text">
<employee id="001" sex="M" age="20">Premshree Pillai</employee>
</company>

我需要获取公司属性样本值

I need to get company attribute sample value

我正在尝试这种方法

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
window.onload = function() {
var currLoanXml = '<company sample="text"><employee id="001" sex="M" age="20">Premshree Pillai</employee></company>';
var pic = $(currLoanXml).find('company').attr('sample');
alert(pic);
};
</script>

它在我的警报框中显示为未定义.

Its Shows Undefined in my alert box.

但是我也可以提醒该子标签正常工作

But i can also alert this child tag its working

var pic = $(currLoanXml).find('employee').attr('id');
alert(pic);  

有什么问题.我需要获取第一个标签属性.请帮我.

What is a problem. I need to get first tag attributes. Please help me.

推荐答案

此处需要使用filter()而不是find(),因为company是根元素,即currLoanXml表示公司元素. find仅查找后继元素

you need to use filter() instead of find() here because company is the root element, ie currLoanXml refers to the company element. find will look for decedent elements only

var currLoanXml = '<company sample="text"><employee id="001" sex="M" age="20">Premshree Pillai</employee></company>';
var pic = $(currLoanXml).filter('company').attr('sample');
alert(pic);

演示:小提琴

这篇关于使用Javascript或Jquery解析xml标记属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆