Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js JSON to XML - serialize a complex object

Tags:

node.js

xml

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?

like image 507
eran otzap Avatar asked Dec 15 '25 04:12

eran otzap


1 Answers

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>
like image 176
Terry Lennox Avatar answered Dec 16 '25 22:12

Terry Lennox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!