使用jQuery迭代XML

时间:2018-07-30 11:13:02

标签: jquery

我是Jquery的新手,并且具有以下xml响应:

<root>
   <case>
      <ckey>100</ckey>
      <case-description>something</case-description>
      <issue>
         <ikey>issue-one</ikey>
         <ikey-description>issue-two</ikey-description>
      </issue>
   </case>
   <case>
      <ckey>101</ckey>
      <case-description>something</case-description>
      <issue>
         <ikey>issue-one</ikey>
         <ikey-description>issue-two</ikey-description>
      </issue>
   </case>
</root>

上述xml的摘要是每个<case>下都有<issue>个节点。我必须遍历每个<case>,然后在其下发出<issue>并将它们添加到可用于填充个案的数组(<case>)中, jQuery数据表中的问题(<issue>)下。

我尝试使用以下代码,但没有获得所需的格式。 有人可以帮我吗?

 $(".dataTables_scrollBody").find(".details-control").on("click", function() {

    var $CaseLkupTables = $(soapGetResultslist.responseXml).find("case");

    $CaseLkupTables.each(function(index, event) {
        var $event = $(event),
            addData = [];

        $event.children().each(function(i, child) {


            addData.push($(child).text());

            var issueNodes = XmlUtil.selectNodes(child, "//issue");
            for (var i = 0; i < issueNodes.length; i++) {

                var idvalue = XmlUtil.xmlToString(XmlUtil.selectSingleNode(issueNodes[i], "//ISSUEID"))
            }
        });

        table.data(addData);
    });

    var xyz = table.row($(this).parents('tr')).data();
    alert("vvv " + xyz[0]);


    var tr = $(this).closest('tr');
    var row = table.row(tr);
    alert("row data " + row.data()[0]);


    $.each(issueNodes, function(index, value) {
        console.log(value)

    });



    console.log(row.data());
    $tds = $row.find("td:nth-child(2)");


    if (row.child.isShown()) {
        // This row is already open - close it
        row.child.hide();
        tr.removeClass('shown');
    } else {
        // Open this row
        var td = $(this).parent('td');
        //alert("Child data "+  table.row( td ).data());
        //  row.child(format(issueNodes) ).show();
        //  expandhideContact( string );

        row.child(issueNodes).show();
        tr.addClass('shown');
    }
});

使用上面的代码,我只能获取案例节点信息,但无法获取每个案例节点下的问题节点信息。

1 个答案:

答案 0 :(得分:1)

尝试此实现

 var x ="<root><case><ckey>100</ckey><case-description>something</case-description><issue><ikey>issue-one</ikey><ikey-description>issue-two</ikey-description></issue></case><case><ckey>101</ckey><case-description>something</case-description><issue><ikey>issue-one</ikey><ikey-description>issue-two</ikey-description></issue></case></root>";

    $($.parseXML(x)).find('case').each(function(index){
                var ckey = $(this).find('ckey').text();
                var description = $(this).find('case-description').text();
                var issueikey = $(this).find('issue').find('ikey').text(); var issueikeyDescription = $(this).find('issue').find('ikey-description').text();
                console.log(ckey,description,issueikey,issueikeyDescription);
            });