Need help parsing XML with jQuery

2019-08-23 00:46发布

<?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>

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.

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.

Any ideas?

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
}

UPDATE - FINAL RESOLUTION BELOW

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 />");
    }
}

2条回答
三岁会撩人
2楼-- · 2019-08-23 01:34

You can handle the mnem thing pretty easily by replacing "client[mnem='c1']" with "client[" + attrname +"='c1']". Was there anything else you needed?

查看更多
我想做一个坏孩纸
3楼-- · 2019-08-23 01:39

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
}
查看更多
登录 后发表回答