0

I am currently using xml parser to convert xml to json.i want to convert xml to json then download json file. My xml is in the form of

    const xml1 = `<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="3.3.5">
  <bpmn:process id="Process_1" isExecutable="true">
  </bpmn:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1">
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn:definitions>

Then my function

async function SaveJson()
    {
     
      var xmlj = new XMLParser().parseFromString(xml1); 
      console.log(xmlj);
      const fileName = "my-file";
      let file = new Blob([xmlj], {
        type: "application/json"
    });
    
     
      const element = document.createElement("a");
      element.href = URL.createObjectURL(file);
      element.download = fileName + ".json";
  
      
      document.body.appendChild(element); 
      element.click();
    }

when i console.log(xmlj) output as

 {name: 'bpmn:definitions', attributes: {…}, children: Array(2), value: '', getElementsByTagName: ƒ}
attributes
: 
{xmlns:bpmn: 'http://www.omg.org/spec/BPMN/20100524/MODEL', xmlns:bpmndi: 'http://www.omg.org/spec/BPMN/20100524/DI', xmlns:dc: 'http://www.omg.org/spec/DD/20100524/DC', id: 'Definitions_1', targetNamespace: 'http://bpmn.io/schema/bpmn', …}
children
: 
(2) [{…}, {…}]
getElementsByTagName
: 
ƒ (e)
name
: 
"bpmn:definitions"
value
: 
""
[[Prototype]]
: 
Object

and the download the json file and open it shows

[object Object]

Any help would be highly appreciated

Shanu k k
  • 1,235
  • 2
  • 18
  • 43
  • maybe blob will not do the job for you, try this: `const jsonString = `data:text/json;chatset=utf-8,${encodeURIComponent( JSON.stringify(xmlj) )}`; const link = document.createElement("a"); link.href = jsonString; link.download = "result.json"; link.click();` – chikabala Jan 05 '23 at 10:26

0 Answers0