I am facing an issue in converting JavaScript object into XML. I have a JavaScript Object with an array inside it, details added below:
var jsonTestData =
{
"ID": 1,
"ApplicationName": "ABC",
"Methods": [
{
"ID": 1,
"Name": "AAA",
"Description": "Test Method",
"Parameters": [
{
"ID": 3,
"Name": "B",
"DefaultValue": "IE"
}
]
},
{
"ID": 2,
"Name": "BBB",
"Description": "s",
"Parameters": [
{
"ID": 4,
"Name": "B",
"DefaultValue": "f"
},
{
"ID": 5,
"Name": "P",
"DefaultValue": "E]]>"
}
]
},
{
"ID": 3,
"Name": "Valid",
"Description": "Some description",
"Parameters": [
{
"ID": 6,
"Name": "ExpectedResult",
"DefaultValue": "0\"]]]>"
}
]
},
{
"ID": 4,
"Name": "Message",
"Description": "Some description",
"Parameters": [
{
"ID": 7,
"Name": "Message",
"DefaultValue": "* First thing* Second thing Sub thing * Third thing"
}
]
},
{
"ID": 5,
"Name": "Result",
"Description": "Some description",
"Parameters": null
},
{
"ID": 6,
"Name": "Verify",
"Description": "Some description",
"Parameters": [
{
"ID": 8,
"Name": "FileToCheck",
"DefaultValue": "1"
}
]
}
]
}
I need to convert the given JavaScript Object to XML, and am able to convert it to XML as well. The JavaScript method that converts above JavaScript Object to XML as shown below,
OBJtoXML = (obj: { [x: string]: any; }) => {
var xml = '';
for (var prop in obj) {
if (prop === "Name") {
} else {
xml += obj[prop] instanceof Array ? '' : "<" + prop + ">";
}
if (obj[prop] instanceof Array) {
for (var array in obj[prop]) {
xml += "<" + prop + ">";
xml += this.OBJtoXML(new Object(obj[prop][array])); //add values
xml += "</" + prop + ">";
}
} else if (typeof obj[prop] == "object") {
xml += this.OBJtoXML(new Object(obj[prop]));
} else {
xml += obj[prop];
}
if (prop === "Name") {
//do nothing
}else if (prop === "Parameters") {
xml += obj[prop] instanceof Array ? '' : "</Parameters>";
xml += "</Name>";
}
else {
xml += obj[prop] instanceof Array ? '' : "</" + prop + ">";
}
}
var xml = xml.replace(/<\/?[0-9]{1,}>/g, '');
return xml
}
Please note that I have another method that removes keys from the JavaScript Object which you don't need to be in the XML. So the converted XML is shown below,
<?xml version="1.0" ?>
<Methods>
<Name>
AAA
</Name>
<Description>
Test Method
</Description>
<Parameters>
<Name>
B
</Name>
<DefaultValue>
IE
</DefaultValue>
</Parameters>
</Methods>
<Methods>
<Name>
BBB
</Name>
<Description>
s
</Description>
<Parameters>
<Name>
B
</Name>
<DefaultValue>
f
</DefaultValue>
</Parameters>
<Parameters>
<Name>
P
</Name>
<DefaultValue>
E]]>
</DefaultValue>
</Parameters>
</Methods>
<Methods>
<Name>
Valid
</Name>
<Description>
Some description
</Description>
<Parameters>
<Name>
ExpectedResult
</Name>
<DefaultValue>
0"]]]>
</DefaultValue>
</Parameters>
</Methods>
<Methods>
<Name>
Message
</Name>
<Description>
Some description
</Description>
<Parameters>
<Name>
Message
</Name>
<DefaultValue>
Result
</DefaultValue>
</Parameters>
</Methods>
<Methods>
<Name>
Result
</Name>
<Description>
Some description
</Description>
<Parameters>
</Parameters>
</Methods>
<Methods>
<Name>
Verify
</Name>
<Description>
Some description
</Description>
<Parameters>
<Name>
FileToCheck
</Name>
<DefaultValue>
1
</DefaultValue>
</Parameters>
</Methods>
Output looks good, however some modifications are required inside 'Parameters' tag. Inside 'Parameters' tag I need to show first child value as the first tag , and 'DefaultValue' tag value must be value of for child tag. for eg: JavaScript Object
{
"ID": 6,
"Name": "Verify",
"Description": "Some description",
"Parameters": [
{
"ID": 8,
"Name": "FileToCheck",
"DefaultValue": "1"
}
]
}
My JavaScript method convert above object to XML and is shown below,
<Name>
Verify
</Name>
<Description>
Some description
</Description>
<Parameters>
<Name>
FileToCheck
</Name>
<DefaultValue>
1
</DefaultValue>
</Parameters>
I want my 'Parameter' section as,
<Parameters> //Name tag value as a tag in the output
<FileToCheck>
1 // 'DefaultValue' tag
</FileToCheck>
</Parameters>
Please note JavaScript Object will always have 'DefaultValue' which I need to show as the value inside the 'Parameter' tag. So could anyone suggest what change I need to make inside my JavaScript method to accomplish the expected result?