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.
我遇到过许多XML 2 Json转换,但没有一个能满足我的需求。在所有这些转换支持中,我得到了一个或另一个错误,因为缺少了一些东西而不支持版本。
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.."
这会将呈现xml的输出抛出为:“http://www.w3.org/2001/XMLSchema-instance”\“xsi:schemaLocation=\”.someurl…
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.
它被"" "包围着。现在我正在尝试将这个输出转换为json,并从主干中呈现出来。用一种简洁的方式做这个演示。但是当我在渲染函数中使用JSON.stringify(this.model)时,输出将是空的。
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?
我的疑问是:是否需要从服务器端I将xml转换为json。从表达e。js或骨干。哪个是最好的?
Thanks in Advance
谢谢提前
1 个解决方案
#1
1
https://github.com/buglabs/node-xml2json sounds like a good place to start.
https://github.com/buglabs/node-xml2json听起来是个不错的起点。
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.
注意,我使用res.send而不是res.json,因为res.json将您提供的任何内容(在本例中是字符串!)转换为JSON。这将导致双重转义。使用res.send使用适当的内容类型可以避免这种情况。
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.
您可以使用object: true从解析器中获取实际的JS对象。但这将意味着服务器上的额外工作:解析器将构建一个对象,res.json将立即再次序列化它。这不是必要的。
Converting on the server side has advantages, since XML handling on the client does not work as well and seamlessly as JSON handling.
在服务器端进行转换是有好处的,因为客户端上的XML处理不如JSON处理那么好和无缝。
#1
1
https://github.com/buglabs/node-xml2json sounds like a good place to start.
https://github.com/buglabs/node-xml2json听起来是个不错的起点。
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.
注意,我使用res.send而不是res.json,因为res.json将您提供的任何内容(在本例中是字符串!)转换为JSON。这将导致双重转义。使用res.send使用适当的内容类型可以避免这种情况。
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.
您可以使用object: true从解析器中获取实际的JS对象。但这将意味着服务器上的额外工作:解析器将构建一个对象,res.json将立即再次序列化它。这不是必要的。
Converting on the server side has advantages, since XML handling on the client does not work as well and seamlessly as JSON handling.
在服务器端进行转换是有好处的,因为客户端上的XML处理不如JSON处理那么好和无缝。