如何将xml节点从一个标签复制或附加到另一个标签

时间:2018-03-07 07:46:56

标签: javascript xml

输入: 我有这样的输入XMl。

<Sports>
    <Country>
        <T "Cricket" = "Yes"></T>
        <T "Footbal" = "Yes"></T>
        <T "TotalCount" = "11"></T>
        <T "TeamName" = "England"></T>
    </Country>
    <Country "Event" = "Yes" >
        <T "TotalCount" = "11" "Event" = "Yes"></T>
        <T "TeamName" = "America"></T>
    </Country>
    <Country "Event" = "Yes" >
        <T "TotalCount" = "11" "Event" = "Yes"></T>
        <T "TeamName" = "Japan"></T>
    </Country>
    <Country "Event" = "Yes" >
        <T "TotalCount" = "11" "Event" = "Yes"></T>
        <T "TeamName" = "Australia"></T>
    </Country>
    <Country "Event" = "Yes" >
        <T "TotalCount" = "11" "Event" = "Yes"></T>
        <T "TeamName" = "NewZeland"></T>
    </Country>
</Sports>

我希望在发送到服务器之前对我的xml进行一些修改。我想要的是我T标记的前两个countary标记是复制到下一个重新发送的countary标记。

<Sports>
    <Country>
        <T "TotalCount" = "11"></T>
        <T "TeamName"= "England"></T>
        <T "Cricket" = "Yes"></T>
        <T "Footbal" = "Yes"></T>
    </Country>
    <Country "Event" = "Yes" >
        <T "TotalCount" = "11" "Event" = "Yes"></T>
        <T "TeamName" = "America"></T>
        <T "Cricket" = "Yes"></T>
        <T "Footbal" = "Yes"></T>
    </Country>
    <Country "Event" = "Yes" >
        <T "TotalCount" = "11" "Event" = "Yes"></T>
        <T "TeamName" = "Japan"></T>
        <T "Cricket" = "Yes"></T>
        <T "Footbal" = "Yes"></T>
    </Country>
    <Country "Event" = "Yes" >
        <T "TotalCount" = "11" "Event" = "Yes"></T>
        <T "TeamName" = "Australia"></T>
        <T "Cricket" = "Yes"></T>
        <T "Footbal" = "Yes"></T>
    </Country>
    <Country "Event" = "Yes" >
        <T "TotalCount" = "11" "Event" = "Yes"></T>
        <T "TeamName" = "NewZeland"></T>
        <T "Cricket" = "Yes"></T>
        <T "Footbal" = "Yes"></T>
    </Country>
</Sports>

这是我在想的。我正在使用第一个标签中可用的额外标签并存储到阵列中。然后我追加到其事件是yas的剩余的数字。但由于理由,它只附加在最后一个标签中。它也是从第一个标签中删除。

我的代码。

var input = some.inputXML.cloneNode(true);
var Countaryele = input.getElementsByTagName('Countary');
var originalTTagelement = Countaryele[0].getElementsByTagName('T');
var temTagEle = [];


for(var i=1; i<Countaryele.length; i++){
    if(Countaryele[i].getAttribute('EVENT') == "Yes"){
        var CountaryTTag = Countaryele[i].getElementsByTagName('T');
        for(var x=0; x<originalTTagelement.length; x++){
            var myNode = originalTTagelement[x];
            var NodeFlg = false;
            for(var y=0; y<CountaryTTag.length; y++){
                if(myNode.isEqualNode(CountaryTTag[y]) == true){
                    NodeFlg = true;
                }
            }
            if(NodeFlg == false){
                temTagEle.push(myNode);
            }
        }

    }
}

这里我附加了元素

var Countaryele = xmlCREle.getElementsByTagName('Countary');
    for(var j=0; j<Countaryele.length; j++){
        if(Countaryele[j].getAttribute('EVENT') == "Yes"){
            for(var k=0; k<temTagEle.length; k++){
                originalKWEle[j].appendChild(temTagEle[k]);
            }
        }
    }

我觉得这有点混乱。欢迎任何建议。

1 个答案:

答案 0 :(得分:2)

根据我在XML输入中看到的内容,XML无效,因为无法正确解析属性。根据您的代码,您似乎用Country错误拼写了标记名Countary

但我们假设它适用于您的情况,我在那里用注释修改了您的代码,只是为了确定发生了什么:

var input = some.inputXML.cloneNode(true);
var Countaryele = input.getElementsByTagName('Countary');
var originalTTagelement = Countaryele[0].getElementsByTagName('T');
var temTagEle = [];

// It doesn't matter if you started from 0 index, the first element doesn't have
// Event element.
for(var i=0; i<Countaryele.length; i++){
    if(Countaryele[i].getAttribute('EVENT') == "Yes"){
        var CountaryTTag = Countaryele[i].getElementsByTagName('T');

        // Let's gether the selected country `T` nodes
        for(var x=0; x< originalTTagelement.length ; x++){
            var myNode = originalTTagelement[x];
            var NodeFlg = false;
            for(var y=0; y<CountaryTTag.length; y++){

                // You want here to check the attribute, not the node
                // Since all the nodes are `T`

                if(myNode.getAttributeNames()[0] == CountaryTTag[y].getAttributeNames()[0]) {
                    NodeFlg = true;
                }

            }
            if(NodeFlg == false) {
                temTagEle.push(myNode);
            }
        }

        // Let's append the missing `T` nodes 
        for(var k=0; k<temTagEle.length; k++){
            var a = temTagEle[k];
            // You can replace `Countaryele` with `originalKWEle` in your case
            Countaryele[i].appendChild(a.cloneNode(true));
            console.log(temTagEle[k]);
        }


        temTagEle = []; // empty it for the next loop
    }
}

我已经收集了收集节点的代码并将它们附加在一起,以防万一你想知道。