0
var usrconf = "<root><node>abc</node><node>efg</node></root>";
var xmlDoc = $.parseXML( usrconf );
$(xmlDoc).find('node').each(function(i) {
    // change node values here
});

Now How can I print the changed XML ?

user1588766
  • 73
  • 1
  • 1
  • 4
  • Basically I need to send back the changes XML to server using ajax POST. But I can't get the changed XML as a string – user1588766 Aug 09 '12 at 21:54

1 Answers1

2

$(this) gives you access to element that is being selected in each(). i is an index of function as described here: http://api.jquery.com/each/

.each( function(index, Element) )

So the correct code is:

var usrconf = "<root><node>abc</node><node>efg</node></root>";
var xmlDoc = $.parseXML( usrconf );
$(xmlDoc).find('node').each(function(i) {
 $(this).attr('changed', true);
});
console.log(xmlDoc);

​Or if you want to serialize it back

var usrconf = "<root><node>abc</node><node>efg</node></root>";
var xmlDoc = $.parseXML( usrconf );
$(xmlDoc).find('node').each(function(i) {
 $(this).attr('changed', true);
});

console.log(xmlDoc);

// from http://stackoverflow.com/a/6507766/141200
function xmlToString(xmlData) { 

    var xmlString;
    //IE
    if (window.ActiveXObject){
        xmlString = xmlData.xml;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else{
        xmlString = (new XMLSerializer()).serializeToString(xmlData);
    }
    return xmlString;
}   

console.log(xmlToString(xmlDoc)); 

​
Jure C.
  • 3,013
  • 28
  • 33