javascript - How to response XML with node.js express? - Stack Overflow

I want to response this using node.js and express:<set id="1" state="0" name=&qu

I want to response this using node.js and express:

<set id="1" state="0" name="wd"/>

I tried:

xml = require('xml');

res.set('Content-Type', 'text/xml');
res.send(xml('<set id="1" state="0" name="wd"/>'));

But in Wireshark i see that my response looks:

&lt;set id=&quot;1&quot; state=&quot;0&quot; name=&quot;wd&quot;/&gt;

How to send my xml correctly?

I want to response this using node.js and express:

<set id="1" state="0" name="wd"/>

I tried:

xml = require('xml');

res.set('Content-Type', 'text/xml');
res.send(xml('<set id="1" state="0" name="wd"/>'));

But in Wireshark i see that my response looks:

&lt;set id=&quot;1&quot; state=&quot;0&quot; name=&quot;wd&quot;/&gt;

How to send my xml correctly?

Share Improve this question edited Mar 23, 2021 at 7:56 Rashomon 6,7924 gold badges39 silver badges80 bronze badges asked May 12, 2019 at 16:52 Kliver MaxKliver Max 5,29924 gold badges101 silver badges157 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

xml package converts JS objects to xml (and you are passing a stringified xml). Not what you need. You need to parse the string containing xml to get actual xml data using libxmljs.

I would try this:

const libxmljs = require("libxmljs");


let stringifiedXml = '<set id="1" state="0" name="wd"/>';
let xmlDoc = libxmljs.parseXml(stringifiedXml);

res.set('Content-Type', 'text/xml');
res.send(xmlDoc);

Try removing xml() function. If you set Content-type, the server would send response as xml.

In ExpressJs Simply add the content-type and send a valid XML document as the xmlContent

response.type('application/xml')
response.send(xmlContent)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742382883a4433470.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信