1/*
2 This code comes from Vincent Lab
3 And it has a video version linked here: https://www.youtube.com/watch?v=5YQXVgNZvRk
4*/
5
6// Import dependencies
7const fs = require("fs");
8const { parseString, Builder } = require("xml2js");
9
10// Load the XML
11const xml = fs.readFileSync("data.xml").toString();
12parseString(xml, function (err, data) {
13
14 // Show the XML
15 console.log(data);
16
17 // Modify the XML
18 data.people.person[0].$.id = 2;
19
20 // Saved the XML
21 const builder = new Builder();
22 const xml = builder.buildObject(data);
23 fs.writeFileSync("data.xml", xml, function (err, file) {
24 if (err) throw err;
25 console.log("Saved!");
26 });
27
28});