How to use the xml2json function from xml-js

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

xml-js.xml2json is a JavaScript function that converts an XML string to a JSON object.

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" });

fork icon0
star icon1
watch icon0

+ 3 other calls in file

221
222
223
224
225
226
227
228
229
    }),
};
axios(url, options)
.then((res) => {
    const result = res.data;
    const strJson = convert.xml2json(result, {compact:true,spaces:4});

    const objJson = JSON.parse(strJson);
    const total = objJson.channel.total._text;
fork icon1
star icon0
watch icon1

+ 2 other calls in file

How does xml-js.xml2json work?

xml-js is a library that allows you to convert XML documents to JSON objects, and xml2json is a function in this library that performs this conversion. When xml2json is called, it takes an XML document as input and parses it into a JSON object, which can then be used in a JavaScript program. The resulting JSON object contains key-value pairs that correspond to the elements and attributes of the original XML document.

4
5
6
7
8
9
10
11
12
13
14
const { join } = require('node:path');
const { xml2json } = require('xml-js');


class FilesTratment {
    TransformXMLToJSON(XMLPathFile) {
        let result = xml2json(readFileSync(XMLPathFile, { encoding: 'utf-8' }), { compact: true });
        return result;
    }
    fixRoute(route) {
        let newData,
fork icon0
star icon2
watch icon1

+ 9 other calls in file

-1
fork icon0
star icon1
watch icon0

+ 2 other calls in file

Ai Example

1
2
3
4
5
6
7
8
const xml2json = require("xml-js");

const xml = " John 30 ";
const options = { compact: true, ignoreComment: true, spaces: 4 };

const json = xml2json.xml2json(xml, options);

console.log(json);

In this example, the xml2json function is called with an XML string xml and an options object options. The compact option is set to true, which means that the resulting JSON will have a more compact format. The ignoreComment option is set to true, which means that any comments in the XML will be ignored. Finally, the spaces option is set to 4, which means that the resulting JSON will be indented with 4 spaces.

16
17
18
19
20
21
22
23
24
25
    console.log(`err => ${err}`)
}
else {
    if(res.statusCode == 200){
        var result = body
        var xmlToJson = convert.xml2json(result , {compact: true, spaces: 4});
        const json = JSON.parse(xmlToJson)
        req.json = json
        next();
    }
fork icon0
star icon0
watch icon1

+ 5 other calls in file

2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
const apiRes = await updateSap(sapData.token, commission);
const updateObj = {
  sapLastTry: moment().format("YYYY-MM-DD HH:mm:ss"),
};
if (!apiRes.error) {
  let result = xmlparser.xml2json(apiRes.data, {
    compact: true,
    spaces: 4,
  });
  result = JSON.parse(result);
fork icon0
star icon0
watch icon1

+ 6 other calls in file

24
25
26
27
28
29
30
31
32
33
var text = fs.readFileSync(copiedFolder + '\\' + myFile, 'utf-8');
var decodeValue = Buffer.from(text, 'base64');
text = decodeValue.toString('utf-8');
fs.writeFileSync(Data_1.Data.folder + '\\OutputDataFromBID.xml', text);
var xml = fs.readFileSync(Data_1.Data.folder + '\\OutputDataFromBID.xml', 'utf-8');
var json = convert.xml2json(xml, { compact: true, spaces: 4 });
fs.writeFileSync(Data_1.Data.folder + '\\OutputDataFromBID.json', json);
fs.rmSync(copiedFolder + '\\' + filename + '.zip');
fs.rmSync(copiedFolder + '\\' + myFile);
fs.rmSync(Data_1.Data.folder + '\\OutputDataFromBID.xml');
fork icon0
star icon0
watch icon0

65
66
67
68
69
70
71
72
73
74
//     let text = fs.readFileSync(copiedFolder + "\\" + myFile, 'utf-8');
//     const decodeValue = Buffer.from(text, 'base64');
//     text = decodeValue.toString('utf-8');
//     fs.writeFileSync("C:\\\\Users\\joung\\OneDrive\\문서\\AutoBID" + "\\OutputDataFromBID.xml", text);
//     const xml = fs.readFileSync("C:\\\\Users\\joung\\OneDrive\\문서\\AutoBID" + "\\OutputDataFromBID.xml", 'utf-8');
//     const json = convert.xml2json(xml, {compact : true, spaces : 4});
//     fs.writeFileSync("C:\\\\Users\\joung\\OneDrive\\문서\\AutoBID" + "\\OutputDataFromBID.json", json);
//     fs.rmSync(copiedFolder + "\\" + filename + ".zip");
//     fs.rmSync(copiedFolder + "\\" + myFile);
//     fs.rmSync("C:\\\\Users\\joung\\OneDrive\\문서\\AutoBID" + "\\OutputDataFromBID.xml");
fork icon0
star icon0
watch icon0

+ 3 other calls in file