I'm converting a JSON to XML String with a java script function. I'm able to even successfully do it. But when I tried to view it in 3 different methods, I get different outputs in .innerHTML when comapred to alert and console.log() What is the reason for such differences? Is there anything wrong that I'm doing.
Here goes my code:
HTML :
<div id="show"></div>
CSS :
#show{
width:100%;
height:200px;
background:#000;
color:#fff;
}
JS
function objectToXml(obj) {
var xml = '';
for (var prop in obj) {
if (!obj.hasOwnProperty(prop)) {
continue;
}
if (obj[prop] == undefined)
continue;
xml += "<" + prop + ">";
if (typeof obj[prop] == "object")
xml += objectToXml(new Object(obj[prop]));
else
xml += obj[prop];
xml += "</" + prop + ">";
}
return xml;
}
var myJson={"id":1,"name":"Albert","dob":"2011-02-19","gender":"male","email":"albert@rediffmail.com","language":"English"}
var xmlString = objectToXml(myJson);
document.getElementById("show").innerHTML=xmlString;
alert(xmlString);
console.log(xmlString);
I don't really understand the reason why XML string is not printed when .innerHTML is used.
Here is a JS FIDDLE