David
David

Reputation: 4271

How to copy or append xml nodes from one tag to another

Input : I have a input XMl like this.

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

I want some modification in my xml before send to the server. What exactly I want is my first two T tag of countary tag is copy to next remaing countary tag.

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

Here what I am trying. I am taking the extra tag which is available in first tag and storing into array. then I appending to the remaing countary whose event is yas. but for reason it is appending only in last tag. Also it is removing from first tag.

My code.

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

    }
}

Here I am appending the element

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]);
            }
        }
    }

I feel that this is bit messay. Any suggestion are welcome.

Upvotes: 1

Views: 48

Answers (1)

Mahdi Fathalla
Mahdi Fathalla

Reputation: 41

Based on what I saw in the XML input which you are received, the XML is not valid since the attributes will not be parsed properly. And based on your code, it seems you misspelled the tag name Country with Countary.

But let's assume it is working in your case, I amended your code with comments there, just to be sure what is going on:

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

I have combined your code of gathering the nodes and appending them together, just in case if you were wondering.

Upvotes: 2

Related Questions