将javascript变量传递给XML主体node.js

时间:2019-02-19 15:42:41

标签: javascript node.js xml

我想将我从发布请求中获得的变量传递到xml主体中,以调用Web服务。如何将JavaScript变量传递给xml?

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

我从发布请求中获取的变量

      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>' + 
  

在这里我想在sonucNo字段中使用变量

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

1 个答案:

答案 0 :(得分:1)

这可以使用template literals完成:

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

console.log(xml);