1

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?

2 Answers 2

1

I have made one change in your method as per your requirement

look at below method and let me know it is useful or not?

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 + ">";
                if (prop === 'Parameters') {
                    var nObj: any = {};
                    nObj[new Object(obj[prop][array])['Name']] = new Object(obj[prop][array])['DefaultValue'];
                    xml += OBJtoXML(nObj); //add values    
                } else {
                    xml += OBJtoXML(new Object(obj[prop][array])); //add values    
                }
                xml += "</" + prop + ">";
            }
        } else if (typeof obj[prop] == "object") {
            xml += 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;
}

I hope it can help let me know...

"use strict";
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"
                }
            ]
        }
    ]
};
var d = OBJtoXML(jsonTestData);
console.log(d);
function OBJtoXML(obj) {
    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 + ">";
                if (prop === 'Parameters') {
                    var nObj = {};
                    nObj[new Object(obj[prop][array])['Name']] = new Object(obj[prop][array])['DefaultValue'];
                    xml += OBJtoXML(nObj); //add values    
                }
                else {
                    xml += OBJtoXML(new Object(obj[prop][array])); //add values    
                }
                xml += "</" + prop + ">";
            }
        }
        else if (typeof obj[prop] == "object") {
            xml += 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;
}

thanks

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much for helping me on this. With the above code change, now am able to create child tags inside 'Parameters' tag with value as well. However tags inside '<Parameters>' is comes as undefined. Now the output is like, '<Parameters> <undefined> 1 </undefined> </Parameters>', instead of '<Parameters> <FileToCheck>1 </FileToCheck> </Parameters>'. So any suggestion from your side?
hi I have checked but i am not getting output like "<Parameters> <undefined> 1 </undefined> </Parameters>" with above json what you have posted let me know which json and which function you are using? thanks
hii please check i have edited answer where i have added snippet run the code snippet and let me know if anything wrong I would like to help you, and also know me if I am putting something wrong thanks.
0

Finally I modified the above method to get the expected result, and fixed code as shown below,

jsonToXml=(obj)=> {
    var xml = '';
    for (let prop in obj) {
      if (prop === "Name") {
      }
      else {
        xml += obj[prop] instanceof Array ? '' : "<" + prop + ">";
      }
      if (obj[prop] instanceof Array) {
        xml += "<" + prop + ">";
        for (let array in obj[prop]) {
          if (prop === 'Parameters') {
            let nObj = {};
            nObj[new Object(obj[prop][array])['Name']] = new Object(obj[prop][array])['DefaultValue'];
            xml += this.jsonToXml(nObj);     
          }
          else {
            xml += this.jsonToXml(new Object(obj[prop][array])); //add values    
          }
        }
        xml += "</" + prop + ">";
      }
      else if (typeof obj[prop] == "object") {
        xml += this.jsonToXml(new Object(obj[prop]));
      } else if (prop === "Name") {
        xml += "<Name=" + '"' + this.jsonToXml(new Object(obj[prop])) + '">';
      }
      else {
        xml += obj[prop];
      }
      if (prop === "Name") {
        //do nothing
      }
      else if (prop === "Parameters") {
        xml += obj[prop] instanceof Array ? '' : "</Parameters>";
        xml += "</Method>";
      }
      else {
        xml += obj[prop] instanceof Array ? '' : "</" + prop + ">";
      }
    }
    var xml = xml.replace(/<\/?[0-9]{1,}>/g, '');
    return xml;
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.