将xml节点的属性设置为变量 [英] set attribute of xml node to a variable

查看:617
本文介绍了将xml节点的属性设置为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取XML节点的属性的值,并使用JQuery将其设置为变量.这可能吗?

I am trying to get the value of an attribute of an XML node and set it as a variable using JQuery. Is this possible?

<DataContainer>
    <Customers>
        <Customer customerId="7366" customerName="Boardwalk Audi" url="" 
            address="5930 West Plano Pkwy" city="Plano" state="Texas" 
            zipCode="75093" latitude="33.0160690000000000" 
            longitude="-96.8268970000000000">
    <Customers>
<DataContainer>

我想将7366的customerId属性设置为变量,以供以后使用,如下所示:

I want to set the customerId attribute of 7366 as a variable for later use like below:

$customerId = customer id from xml node attribute;

这可能吗?让我知道是否需要进一步说明.谢谢!

Is this possible? Let me know if you need any more clarification. Thanks!

推荐答案

如果您要在ajax请求中获取XML,则可以使用 .find() 获取节点和 .attr() 获取属性,例如:

If you're fetching the XML in an ajax request you can just use .find() to get the node and .attr() to get the attriute, for example:

var cust_id = $(xml).find("Customer").attr("customerId");

您可以在此处看到一个示例,请记住,该示例旨在用于最终使用responseXML(不仅是XML字符串)进行ajax回调,但是您有了主意:)

You can see an example here, keep in mind this is intended to be used in an ajax callback ultimately using responseXML (not just an XML string), but you get the idea :)

这篇关于将xml节点的属性设置为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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