jQuery读取xml文件

转自http://www.cnblogs.com/huacw/archive/2011/03/24/1994074.html

<?xml version="1.0" encoding="utf-8" ?>
 <taxrates>
  <taxrate id="1">
    <lower>0</lower>
    <upper>500</upper>
    <rate>5</rate>
    <buckle>0</buckle>
  </taxrate>
  <taxrate id="2">
    <lower>500</lower>
    <upper>2000</upper>
    <rate>10</rate>
    <buckle>25</buckle>
  </taxrate>
  <taxrate id="3">
    <lower>2000</lower>
    <upper>5000</upper>
    <rate>15</rate>
    <buckle>125</buckle>
  </taxrate>
  <taxrate id="4">
    <lower>5000</lower>
    <upper>20000</upper>
    <rate>20</rate>
    <buckle>375</buckle>
  </taxrate>
  <taxrate id="5">
    <lower>20000</lower>
    <upper>40000</upper>
    <rate>25</rate>
    <buckle>1375</buckle>
  </taxrate>
  <taxrate id="6">
    <lower>40000</lower>
    <upper>60000</upper>
    <rate>30</rate>
    <buckle>3375</buckle>
  </taxrate>
  <taxrate id="7">
    <lower>60000</lower>
    <upper>80000</upper>
    <rate>35</rate>
    <buckle>6375</buckle>
  </taxrate>
  <taxrate id="8">
    <lower>80000</lower>
    <upper>100000</upper>
    <rate>40</rate>
    <buckle>10375</buckle>
  </taxrate>
  <taxrate id="9">
    <lower>100000</lower>
    <upper>99999999</upper>
    <rate>45</rate>
    <buckle>15375</buckle>
  </taxrate>
</taxrates>


读取的函数

function StandardTaxRate()
{
    $.ajax({
        url: "/Resource/salaryTaxRate.xml",
        dataType: 'xml',
        type: 'GET',
        timeout: 2000,
        error: function(xml)
        {
            alert("加载XML 文件出错!");
        },
        success: function(xml)
        {
            $(xml).find("taxrate").each(function(i)
            {
                var oid = $(this).attr("id");
                var lower = $(this).children("lower").text();
                var upper = $(this).children("upper").text();
                var rate = $(this).children("rate").text();
                var buckle = $(this).children("buckle").text();
                ///后续操作。。。
            });
        }
    });
}

自己实际应用中的修改

		function setHtmlStyle(){
		 $.ajax({
			url: "Chart_Oilproduction_well_Standard_Pattern.XML",
			dataType: 'xml',
			type: 'GET',
			timeout: 2000,
			error: function(xml)
			{
				alert("加载XML 文件出错!");
			},
			success: function(xml)
			{
				$(xml).find("charts").find("charts_title").find("style").each(function(i)
				{
					var lower = $(this).children("font_family").text();
					alert (lower);
				});
			}
		   });
		}

这种根据一级一级的找非常方便

$(xml).find("charts").find("charts_title").find("style").each(function(i)
				{
					var lower = $(this).children("font_family").text();
					alert (lower);
				});

另外根据某个item的名称来找全局的也很方便,方法如下:

寻找全局style的item下面的font_family属性节点

success: function(xml)
			{
				$(xml).find("style").each(function(i)
				{
					var lower = $(this).children("font_family").text();
					alert (lower);
				});
			}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值