2

I am a newbie into Node.js

I have come across many XML 2 Json Conversions, None of them satisfied my needs up to the mark. Out of all this conversion supports, i get one or the other error as something is missing and don't support to the version.

Here is my code :

 exports.index = function(req, res){    
   var request = require('request');
   var url = 'xml-output-throwing-url';

     request(url, function (error, response, body) {
        if (!error && response.statusCode == 200) { 
           res.json(body);
        }
     });
 };

This throws output of rendering the xml as : "http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"..someurl...">..Output.."

Which is enclosed by "". Now i am trying to convert this output to json and render this from backbone.js and make this presentation in a neat way. But the output will be empty when i am using JSON.stringify(this.model) in the render function.

What am I doing wrong here?

How to achieve this?

My doubts are : Do I need to convert xml to json from server side i.e from express.js or backbone. Which is best among this?

Thanks in Advance

1 Answer 1

1

https://github.com/buglabs/node-xml2json sounds like a good place to start.

var parser = require('xml2json');

// ...

exports.index = function(req, res){    
   var request = require('request');
   var url = 'xml-output-throwing-url';

     request(url, function (error, response, body) {
        if (!error && response.statusCode == 200) { 
           var result = parser.toJson(body, {
              object: false,
              reversible: false,
              coerce: true,
              sanitize: true,
              trim: true,
              arrayNotation: false
           });
           res.set('Content-Type', 'application/json');
           res.send(result);
        }
    });
};

Note that I use res.send instead of res.json because res.json converts whatever you give it (a string, in this case!) to JSON. This would result in double escaping. Using res.send with the appropriate content type avoids this.

You could use object: true to get an actual JS object out of parser.toJson, but that would mean extra work on the server: The parser would build an object and res.json would immediately serialize it again. That's not necessary.

Converting on the server side has advantages, since XML handling on the client does not work as well and seamlessly as JSON handling.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you so much for the valuable feedback. I am keep trying this xml2json from past 2 days. But i get this ERROR I have tried to set environmental variable for python as well and restarted my system as well. But still not working. Please assist with this..
Well, node-xml2json has a dependency to node-expat. Have you installed that module?
Thanks for the reply. I get this Error when i try to install on which u suggested . And I have worked on many with npms with in this couple of days. I keep getting errors like : "No repository field". Does that mean its still in progress or removed or in the stage of development or am i doing wrong in the installation :P
node-expat requires Python in turn. ;) If you don't want to install Python just because of this (and maybe that's really overkill), try another XML-to-JS converter. npmjs.org/package/xml2js would be worth a try.
Thanks i managed to get this done with the combination of efforts. It killed me for last two days. Last but not the least, the output is been truncated or toggled and displayed as objects for the child nodes. So how to expand this and see the clear result and how to handle this from backbone. Thanks in Advance :)
|

Your Answer

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