JSON to XML Using Javascript

前端 未结 6 937
南旧
南旧 2020-12-11 09:25

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 converti

6条回答
  •  -上瘾入骨i
    2020-12-11 10:04

    replace your OBJtoXML function with

    function OBJtoXML(obj) {
      var xml = '';
      for (var prop in obj) {
        xml += obj[prop] instanceof Array ? '' : "<" + prop + ">";
        if (obj[prop] instanceof Array) {
          for (var array in obj[prop]) {
            xml += "<" + prop + ">";
            xml += OBJtoXML(new Object(obj[prop][array]));
            xml += "";
          }
        } else if (typeof obj[prop] == "object") {
          xml += OBJtoXML(new Object(obj[prop]));
        } else {
          xml += obj[prop];
        }
        xml += obj[prop] instanceof Array ? '' : "";
      }
      var xml = xml.replace(/<\/?[0-9]{1,}>/g, '');
      return xml
    }
    

提交回复
热议问题