'How do I append this xml object to xml file
app.post('/data', (req, res) => {
var bdy = req.body;
var doc = builder.create('event')
.att('id', '69420')
.ele('title').txt(bdy.title).up()
.ele('type').txt(bdy.type).up()
.ele('date',).att('repeat', 'weekly')
.ele('day').txt(bdy.dates[0]).up()
.ele('month').txt(bdy.dates[1]).up()
.ele('startingTime').txt(bdy.dates[2]).up()
.ele('endingTime').txt(bdy.dates[3]).up().up()
.ele('guests')
.ele('guest')
.ele('name').text(bdy.guests[0].name).up()
.ele('email').text(bdy.guests[0].email).up().up()
.ele('guest')
.ele('name').text(bdy.guests[1].name).up()
.ele('email').text(bdy.guests[1].email).up().up().up()
.ele('venue').txt(bdy.venue).up()
.ele('description').txt(bdy.description);
const xml = doc.end({ prettyPrint: true });
var appendxml = xml.substr(xml.indexOf('>')+1);
fs.appendFile(dataFile,appendxml, (err) => {
if(err)
throw err;
})
fs.readFile(dataFile, (err, rdata) => {
res.send(rdata);
})
})
So when I execute this code it basically appends my new XML object after the root like in the image provided the root is schedule and schedule can hold multiple events, so my goal is to append my new XML object which I created above to be a valid part of the XML document. when I try do replace or substring my data is basically just says there is no such function for data im guessing cuz it is xml and not string
Solution 1:[1]
Using Python, we can achieve this in multiple ways,
a = ['a','b','c']
b = ['d','e']
c = ['f','g']
Using Multiple FOR Loops
for i in a: for j in b: for k in c: print([i, j, k])
Output:
['a', 'd', 'f']
['a', 'd', 'g']
['a', 'e', 'f']
['a', 'e', 'g']
['b', 'd', 'f']
['b', 'd', 'g']
['b', 'e', 'f']
['b', 'e', 'g']
['c', 'd', 'f']
['c', 'd', 'g']
['c', 'e', 'f']
['c', 'e', 'g']
Using Lists Comprehensions:
d = [[i, j, k] for i in a for j in b for k in c] d
** Output:**
[['a', 'd', 'f'],
['a', 'd', 'g'],
['a', 'e', 'f'],
['a', 'e', 'g'],
['b', 'd', 'f'],
['b', 'd', 'g'],
['b', 'e', 'f'],
['b', 'e', 'g'],
['c', 'd', 'f'],
['c', 'd', 'g'],
['c', 'e', 'f'],
['c', 'e', 'g']]
We can also use itertools module to generate the permutation combinations.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gowtham Jayachandiran |

