How to take data from a specific field in an XML,with Nodejs,and then save it in the Mongo database

Asked

Viewed 368 times

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!

  • 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!

  • It is, with a small change. But later I will test and come back with the results.Thank you very much!

  • 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.

  • I edited my answer

  • 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!

Show 1 more comment

2 answers

1

alternative...

For those who want to avoid using regex and looking for an alternative solution using some specific library, we can use in Node.JS the fast-xml-parser:

Installation:

npm install fast-xml-parser

Use:

const parser = require('fast-xml-parser');

const jsonObj = parser.parse(response); // "entrada"

Now we can access properties like in a Javascript object:

console.log(jsonObj.session.id); // 1b6b0de9dd6549ebb5e0142ddd95facd

Example of using a CDN and the XML of the question as an example:

const response =
  '<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>' +
  '<session>' +
  '<id>1b6b0de9dd6549ebb5e0142ddd95facd</id>' +
  '</session>';

const responseObj = parser.parse(response);

console.log('dados do "response":', responseObj);
console.log('ID da "session":', responseObj.session.id);
<script src="https://cdnjs.cloudflare.com/ajax/libs/fast-xml-parser/3.18.0/parser.min.js"></script>

1


If you want to use a solution that uses regular expression, you can try the following code:

const entrada = `<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<session>
    <id>1b6b0de9dd6549ebb5e0142ddd95facd</id>
</session>`
const regexexp = /<id>(.+?)<\/id>/
const match = regexexp.exec(entrada)

Using this regular expression, it will capture 2 possible expressions for the past input. Leaving the values available in an array. It is possible to recover 2 values.

console.log(match[0])
//"<id>1b6b0de9dd6549ebb5e0142ddd95facd</id>"
console.log(match[1])
//"1b6b0de9dd6549ebb5e0142ddd95facd"

You can learn more about how the match function here

To implement this functionality in your code, it would be something like:

.then(function(response) {
  const regexexp = /<id>(.+?)<\/id>/
  const sessionMatch = regexexp.exec(response.data)
  const sessionId = sessionMatch[1];

  const session_Id = new MyModelMongo({

    sessionId,

  });

  session_Id.save();
  return res.send(response.data);
})
  • Perfect! Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.