4

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?

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

When i tried to console.log(xmlDoc) i got Document{errors:[]}. What can i miss?
I think that console.log(xmlDoc) doesnt support xml data. Maybe you can try console.dirxml(xmlDoc) nodejs.org/api/console.html#console_console_dirxml_data
1

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

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.