0
In an API response, it brings me an XML, but I need to capture just a few specific fields and save it in my database Mongodb with the package Mongoose,using Nodejs and Axios.
My XML response comes like this:
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<session>
<id>1b6b0de9dd6549ebb5e0142ddd95facd</id>
</session>
This is the part of my code that returns/prints the XML response above:
.then(function(response) {
//console.log(JSON.stringify(response.data));
//return res.json(response.data);
const sessionId = response.data;
const session_Id = new MyModelMongo({
sessionId,
});
session_Id.save();
//return res.json(session_Id.id);
//return res.json(session_Id.sessionId);
return res.send(response.data);
})
I need to return/print only one specific field, id that is inside Session,that are present in the XML API response, and save it in the database.I’ve never worked with xml and I’m having trouble returning and saving only that field id.
Thanks in advance!
I believe I will have to download a package for this,during my researches I did not find as JS/Node native way to do this. I’ll be back with the results!
– Guillerbr
Of course, but at first I noticed that you saved the static answer in const = input, but since it is a return of the API,this id field,will be different with each new answer. I’m thinking here,I think with a small change would work.I’ll try! OBG!
– Guillerbr
It is, with a small change. But later I will test and come back with the results.Thank you very much!
– Guillerbr
Really with your example I was able to print each dynamic id according to each answer. However I need to save this data in my bank. And not being able to isolate only match[1] in a variable to later save to the database, still saving the full xml as before.Could you help me at this point? I noticed that when printing the match[1] it responds correctly to the id, but still need to be treated and saved in the bank.
– Guillerbr
I edited my answer
– Danizavtz
Perfect Blessed One! I’ve always been terrible with regex,and I’m going to have to reread it here.Overall it helped a lot, I managed to do the whole process perfectly,many people advised me to install a Node package, but I knew that a smart person could solve it.Thanks!
– Guillerbr