3

I'm attempting to convert a complex JSON object to XML using this package - XML.

 var xml = require("xml");
 var obj = {  MS : { ts : 3423523, isOk : false , errors : [] }};
 var xmlObj = xml(obj); //  This outputs <MS/> 

Any ideas how to make the XML parser go deeper? Why is it prematurely closed?

2
  • 3
    "Values can be an array of xmlObjects or a value such as a string or number." - not an object. Commented Aug 26, 2020 at 8:16
  • @jonrsharpe so any why to serialize a complex object, I can iterate over it myself i guess Commented Aug 26, 2020 at 8:26

1 Answer 1

2

You could give the xml2js module a try, this would convert your object to Xml quite easily, e.g.

const xml2js = require('xml2js');

const obj = {  MS : { ts : 3423523, isOk : false , errors : [] }};

const builder = new xml2js.Builder( { headless: false, renderOpts: { pretty: true }  });
const xml = builder.buildObject(obj);

console.log(xml)

The output would be:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MS>
  <ts>3423523</ts>
  <isOk>false</isOk>
</MS>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'll try this later on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.