需要帮助使用jQuery解析XML [英] Need help parsing XML with jQuery

查看:81
本文介绍了需要帮助使用jQuery解析XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?xml version="1.0"?>
<watchlist timestamp="2013-02-04 17:38:24">
  <team name="Parent">
      <child name="ChildTeam1" team="3">
          <client mnem="c1">5</client>
          <client mnem="c2">0</client>
          <client mnem="c3">1</client>
          <client mnem="c4">1</client>
          <client mnem="c5">2</client>
          <client mnem="c6">6</client>
          <client mnem="c7">0</client>
          <client mnem="c8">0</client>
          <client mnem="c9">1</client>
          <client mnem="c10">0</client>
      </child>
      <child name="ChildTeam2" team="3">
          <client mnem="c1">6</client>
          <client mnem="c2">0</client>
          <client mnem="c3">0</client>
          <client mnem="c4">0</client>
          <client mnem="c5">0</client>
          <client mnem="c6">0</client>
          <client mnem="c7">0</client>
          <client mnem="c8">0</client>
          <client mnem="c9">0</client>
          <client mnem="c10">0</client>
      </child>
  </team>
</watchlist>

我需要使用jQuery解析上述XML的帮助.有家长小组和儿童小组.我的目标是将团队ID为3的父团队的总c1加起来...所以在上面的示例中,要获得c1的总和,我将在ChildTeam1中找到5,在ChildTeam2中找到6对于c1总共得到11.

I need help parsing the above XML using jQuery. There are parent teams with children teams under them. My goal is to add up the total c1 for the parent team who's team id is 3... so in the example above, to get the total for c1, I'd add the 5 found in ChildTeam1 and the 6 found in ChildTeam2 go get a total of 11 for c1.

要添加一个扭曲...客户端上的mnem属性将定期更改,因此我无法对"c1"进行硬编码过滤,必须动态提取该部分.我确实知道,一个孩子之下总会有10位客户.

To add a twist... the mnem attribute on the client will change periodically, so I cannot hard code to filter for "c1", I have to dynamically pull that part. I do know that there will always be exactly 10 clients under a child though.

有什么想法吗?

到目前为止,我的代码确实正确计算了第3组的c1总数,并为mnem属性使用了一个过滤器(由于mnem的改变,我正试图摆脱它):

The code I have so far, which does calculate the total for c1 for team 3 correctly, uses a filter for the mnem attribute (which I'm trying to get away from since the mnem changes):

function parseXml(data) {
var count1 = 0;
    $(data).find("child[team='3']").each(function(child) {
        count1 += parseInt($(this).find("client[mnem='c1']").text(), 10);
    });
    alert(count1); //output total c1 for team 3
}


更新-最终解决方案如下

function parseXml(data) {
    var i = 0; 
    var mnemonic = new Array();

    //build array of the 10 mnemonics, with mnemonic as key value
    $(data).find("client").each(function(){ 
        if ( i < 10 ) {
            mnemonic[$(this).attr('mnem')] = 0; //set default count to 0
            i++;
        } else { return false; } //break .each loop
    });

    //find all children where team = 3, and add the numbers up for each client mnemonic
    $(data).find("child[team='3']").each(function() {
        for(var index in mnemonic) {
            //add to the count
            mnemonic[index] +=     parseInt($(this).find("client[mnem="+index+"]").text(), 10);
        }
    });

    //output the results
    for(var index in mnemonic) {
        $("#output").append( index + " : " + mnemonic[index] + "<br />");
    }
}

推荐答案

您是否有麻烦将c1更改为其他内容?只需在选择器中编辑字符串即可.

Are you just having trouble change c1 to something else? That's just a matter of editing the string in your selector:

function parseXml(data) {
    var count = 0,
        teamnum = 3,
        counter = 'c1';
    $(data).find("child[team="+count+"]").each(function(i,el) {
        count1 += parseInt($(this).find("client[mnem="+counter+"]").text(),10);
    });
    alert(count1); //output total c1 for team 3
}

这篇关于需要帮助使用jQuery解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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