How to use xml-js

Comprehensive xml-js code examples:

How to use xml-js.json2xml:

83
84
85
86
87
88
89
90
91
92
  data: {
    recipe: recipes
  }
};

const xml = xmljs.json2xml(exportData, { compact: true, ignoreComment: true, spaces: 4 });

if (req.query.download === 'true') res.setHeader('Content-disposition', `attachment; filename=recipesage-data-${Date.now()}.xml`);
res.setHeader('Content-type', 'text/xml');
res.write(xml);

How to use xml-js.js2xml:

594
595
596
597
598
599
600
601
602
    return unit.serialize();
});

// logger.trace("json is " + JSON.stringify(json, undefined, 4));

var xml = '<?xml version="1.0" encoding="utf-8"?>\n' + xmljs.js2xml(json, {
    compact: true,
    spaces: 2
});

How to use xml-js.xml2json:

0
1
2
3
4
5
6
7
8
9
const pth = require('path');
const fs = require('fs');
const axios = require('axios');
const { xml2json, json2xml } = require('xml-js');
const xml = fs.readFileSync(pth.join(__dirname, "/defdata.xml"), { encoding: "utf8" });
const cjson = xml2json(xml, { spaces: 4, ignoreComment: true, compact: true });
const cxml = json2xml(cjson, { compact: true });
const apiLinks = fs.readFileSync(pth.join(__dirname, "/apilinks.json"), { encoding: "utf8" });
const mlinks = fs.readFileSync(pth.join(__dirname, "/links.json"), { encoding: "utf8" });

How to use xml-js.xml2js:

71
72
73
74
75
76
77
78
79
80
async getContent_() {
  /** @const {StorageFileConfig} */
  const { bucket, name, projectId } = this.config.source;
  const xml = await StorageFile.getInstance(bucket, name,
    { projectId: this.getCloudProject() }).loadContent(0);
  const data = xml2js(xml, { compact: true });
  const items = data.rss.channel.item.map((item) => {
    const result = {};
    Object.keys(item).forEach((key) => {
      const keyFragments = key.split(':');