How to use the json2xml function from xml-js

Find comprehensive JavaScript xml-js.json2xml code examples handpicked from public code repositorys.

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);
fork icon30
star icon354
watch icon0

697
698
699
700
701
702
703
704
705
706
case 'json':
  data = JSON.stringify(recipes_j);
  mimetype = 'application/json';
  break;
case 'xml':
  data = xmljs.json2xml(recipes_j, { compact: true, ignoreComment: true, spaces: 4 });
  mimetype = 'text/xml';
  break;
case 'txt':
  data = '';
fork icon30
star icon354
watch icon0

1
2
3
4
5
6
7
8
9
10
11
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" });


function writeMyFile(fname = "data.json", fcnt = "") {
fork icon0
star icon1
watch icon0

45
46
47
48
49
50
51
52
53
54
55
56
    return JSON.parse(parser.xml2json(fs.readFileSync(path.resolve(filepath), 'utf-8'), parseOpts));
};


Utilities.writeJsonToXmlFile = function(jsonObj, filepath, parseOpts){
    parseOpts = parseOpts || {compact: true, spaces: 4};
    var xmlStr = parser.json2xml(JSON.stringify(jsonObj), parseOpts);
    fs.writeFileSync(path.resolve(filepath), xmlStr);
};


/**
fork icon1
star icon0
watch icon0

34
35
36
37
38
39
40
41
42
43
    // Setting.GetData()
};
BidHandling.JsonToBid = function () {
    var resultFilePath = Data_1.Data.folder + '\\OutputDataFromBID.json';
    var json = fs.readFileSync(resultFilePath, 'utf-8');
    var xml = convert.json2xml(json, { compact: true, ignoreComment: true, spaces: 4 });
    fs.writeFileSync(Data_1.Data.folder + '\\Result_Xml.xml', xml);
    var text = fs.readFileSync(Data_1.Data.folder + '\\Result_Xml.xml', 'utf-8');
    var encodeValue = Buffer.from(text, 'utf-8');
    text = encodeValue.toString('base64');
fork icon0
star icon0
watch icon0

80
81
82
83
84
85
86
87
88
89
90
91


    return jsonObj;
  },
  convertToXML: function convertToXML(jsonObj) {
    let json = JSON.stringify(jsonObj);
    let xml = convert.json2xml(json, { compact: true, spaces: 4 });


    return xml;
  },
};
fork icon0
star icon0
watch icon0

31
32
33
34
35
36
37
38
39
40
    fs.rmSync("C:\\\\Users\\joung\\OneDrive\\문서\\AutoBID" + "\\OutputDataFromBID.xml");
};
BidHandling.JsonToBid = function () {
    var resultFilePath = "C:\\\\Users\\joung\\OneDrive\\문서\\AutoBID\\OutputDataFromBID.json";
    var json = fs.readFileSync(resultFilePath, 'utf-8');
    var xml = convert.json2xml(json, { compact: true, ignoreComment: true, space: 4 });
    fs.writeFileSync("C:\\\\Users\\joung\\OneDrive\\문서\\AutoBID" + "\\Result_Xml.xml", xml);
    var text = fs.readFileSync("C:\\\\Users\\joung\\OneDrive\\문서\\AutoBID\\Result_Xml.xml", 'utf-8');
    var encodeValue = Buffer.from(text, 'utf-8');
    text = encodeValue.toString('base64');
fork icon0
star icon0
watch icon0

+ 3 other calls in file

20
21
22
23
24
25
26
27
28
29
    delete Object.assign(nodeJson, { label: nodeJson.text })['text'];
    Object.keys(nodeJson.customProperties).forEach(key=>{
        nodeJson['MR_'+key]=nodeJson.customProperties[key]
    })
    delete nodeJson['customProperties']
    const newNode = {object: {"_attributes":{...nodeJson}, "_text": parser.json2xml(style)}}
    let xml = convertJS2XML(newNode, true)
    return xml
}
function getMRAttributes(attributes){
fork icon0
star icon0
watch icon0

+ 3 other calls in file

47
48
49
50
51
52
53
54
55
56
  dataCount = await Product.countDocuments(filter);
} else {
  dataCount = await Product.countDocuments();
}
const json = JSON.stringify(data);
const xml = json2xml(json, { compact: true, spaces: 1 });

res.header("Content-Type", "application/xml");
res.status(200).send({
  data: xml,
fork icon0
star icon0
watch icon0

+ 3 other calls in file