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 ?
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 ?
$(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));