Very surprised that I couldn't find anything on the Net for this that was straight forward and simple.
Here is how I convert any javascript object to XML. I use it to send data from javascript to web services.
function toXml(obj, name) {
var s = "<" + name + ">";
if (typeof (obj) == "object" || typeof (obj) == "array") {
for (var property in obj) {
var pName = property;
if (!isNaN(pName)) pName = name + pName;
s += toXml(obj[property], pName);
}
}
else {
s += obj.toString()
.replace(/\&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/\'/g, ''')
.replace(/\"/g, '"');
}
s += "</" + name + ">";
return s;
}