Ilker Kadir Ozturk
Ilker Kadir Ozturk

Reputation: 362

Passing a javascript variable to XML body node.js

I would like to pass the variable that I get from a post request into xml body to make a call to a webservice. How do I pass javascript variables to xml ?

router.post('/', async (req, res) => {
const sorguNo= req.body.sorguNo; 

the variable that I get from post request

      const url = 'Url';
       const headers = {
      'Content-Type': 'text/xml; charset=utf-8',
      'soapAction': 'Soap Action'
         };
    const xml = '<?xml version="1.0" encoding="utf-8"?>'+
    '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
      '<soap:Header>' +
        '<AuthHeader xmlns="http:">' +
          '<userName>username</userName>' +
           '<password>password</password>' +
        '</AuthHeader>' +
      '</soap:Header>' +
    '<soap:Body>' +
        '<xmlns="http:..">' +
          '<sonucNo></sonucNo>' + 

here I would like to use the variable in sonucNo field

        '</>' +
      '</soap:Body>' +
    '</soap:Envelope>';

Upvotes: 1

Views: 3455

Answers (1)

sn3p
sn3p

Reputation: 746

This can be done using template literals:

const sonucNo = "hello";
const xml = `<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope>
    <sonucNo>${sonucNo}</sonucNo>
  </soap:Envelope>`;

console.log(xml);

Upvotes: 2

Related Questions