Json To Xml Using Javascript
I am trying to convert the JSON to XML but not getting exact output.In My JSON having array object it not converting that to XML array.Mainly array object is not converting into
Solution 1:
replace your OBJtoXML
function with
functionOBJtoXML(obj) {
var xml = '';
for (var prop in obj) {
xml += obj[prop] instanceofArray ? '' : "<" + prop + ">";
if (obj[prop] instanceofArray) {
for (var array in obj[prop]) {
xml += "<" + prop + ">";
xml += OBJtoXML(newObject(obj[prop][array]));
xml += "</" + prop + ">";
}
} elseif (typeof obj[prop] == "object") {
xml += OBJtoXML(newObject(obj[prop]));
} else {
xml += obj[prop];
}
xml += obj[prop] instanceofArray ? '' : "</" + prop + ">";
}
var xml = xml.replace(/<\/?[0-9]{1,}>/g, '');
return xml
}
Solution 2:
There are a few problems here, for starters, here the JSON string variable either needs to have it's quotes escaped. Or be wrapped in single quotes. For example:
varInputJSON= '{"body":{"entry": [{ "fullURL" : "abcd","Resource": "1234"},{ "fullURL" : "efgh","Resource": "5678"}]}}';
Next, there is no need to use eval
here, when using JSON in JavaScript you should use JSON.parse
// First parse the JSONvarInputJSON = JSON.parse(InputJSON);
// Now execute the 'OBJtoXML' functionvar output = OBJtoXML(InputJSON);
Now we come to the meat of this question, why is entry
only occuring once?
The problem that you're having is that xml += "<" + prop + ">";
and xml += "</" + prop + ">";
are only happening once per property.
A possible solution would look like this:
functionOBJtoXML(obj) {
var xml = '';
for (var prop in obj) {
xml += "<" + prop + ">";
if(Array.isArray(obj[prop])) {
for (var array of obj[prop]) {
// A real botch fix here
xml += "</" + prop + ">";
xml += "<" + prop + ">";
xml += OBJtoXML(newObject(array));
}
} elseif (typeof obj[prop] == "object") {
xml += OBJtoXML(newObject(obj[prop]));
} else {
xml += obj[prop];
}
xml += "</" + prop + ">";
}
var xml = xml.replace(/<\/?[0-9]{1,}>/g,'');
return xml
}
Solution 3:
Using xml-js lib
import { json2xml } from"xml-js";
const input = {
contact: {
name: `John & cia "example"`
}
};
const xml = json2xml(input, {
compact: true
});
// <contact><name>John & cia \"example\"</name></contact>
https://codesandbox.io/s/xml-json-forked-zgit4?file=/src/index.js:97-103
:)
Solution 4:
Xml-to-json library has method jsonToXml(json)
.
var inputJSON = '{"body":{"entry": [{ "fullURL" : "abcd","Resource": "1234"},{ "fullURL" : "efgh","Resource": "5678"}]}}';
var xml = jsonToXml(inputJSON);
// <?xml version="1.0" encoding="UTF-8"?>
// <body>
// <entry>
// <fullURL>abcd</fullURL>
// <Resource>1234</Resource>
// </entry>
// <entry>
// <fullURL>efgh</fullURL>
// <Resource>5678</Resource>
// </entry>
// </body>
Solution 5:
var inputJSON = '{"body":{"entry": [{ "fullURL" : "abcd","Resource": "1234"},{ "fullURL" : "efgh","Resource": "5678"}]}}';
var parsedInput = JSON.parse(inputJSON);
functionOBJtoXML(obj) {
var xml = '';
for (var prop in obj) {
if (obj[prop] instanceofArray) {
for (var array in obj[prop]) {
xml += '<' + prop + '>';
xml += OBJtoXML(newObject(obj[prop][array]));
xml += '</' + prop + '>';
}
} else {
xml += '<' + prop + '>';
typeof obj[prop] == 'object' ? xml += OBJtoXML(newObject(obj[prop])) : xml += obj[prop];
xml += '</' + prop + '>';
}
}
var xml = xml.replace(/<\/?[0-9]{1,}>/g, '');
return xml;
}
Post a Comment for "Json To Xml Using Javascript"