【问题标题】:Expressjs response as JSON and XmlExpressjs 响应为 JSON 和 Xml
【发布时间】:2014-02-19 09:30:25
【问题描述】:

我在做什么:: 我正在尝试为数据库中的数据集生成 json 和 xml 输出

Express Code:: 这里我正在尝试 JSON 响应

var express = require('express')
  , async = require('async')
  , http = require('http')
  , mysql = require('mysql'); 
  var xml = require('xml');

var app = express();

var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    database: 'MyDatabase'
});

connection.connect();

// all environments
app.set('port', process.env.PORT || 3007);

app.use(express.static(__dirname + '/public/images'));

app.get('/Result/',function(request,response){
    var name_of_restaurants;

    async.series( [
        // Get the first table contents
        function ( callback ) {
            connection.query('SELECT * FROM mas_buf_type', function(err, rows, fields)
                {
                        console.log('Connection result error '+err);
                        name_of_restaurants = rows;
                        callback();
                });

 }

   // Send the response
], function ( error, results ) {


 response.json({'restaurants' : name_of_restaurants });



//response.set('Content-Type', 'text/xml');
//response.send(xml(name_of_restaurants));
} );

} );




http.createServer(app).listen(app.get('port'),function(){
        console.log('Express server listening on port'+app.get('port'));
});

我的输出::

{
    "restaurants": [
        {
            "Buf_Type_Id": 1,
            "Buf_Type_Name": "Breakfast"
        },
        {
            "Buf_Type_Id": 2,
            "Buf_Type_Name": "Lunch"
        },
        {
            "Buf_Type_Id": 3,
            "Buf_Type_Name": "Dinner"
        }
    ]
}

现在代替

 response.json({'restaurants' : name_of_restaurants });

我添加了这些行以获得XML 输出

response.set('Content-Type', 'text/xml');
response.send(xml(name_of_restaurants));

输出::

<Buf_Type_Id>1</Buf_Type_Id>
<Buf_Type_Id>2</Buf_Type_Id>
<Buf_Type_Id>3</Buf_Type_Id>

我的问题::

  • 很明显,你可以看到两个输出不同,我找不到 xml 输出中的第二列
  • 如何解决此问题
  • 我需要进行哪些更改

{编辑}

var express = require('express')
  , async = require('async')
  , http = require('http')
  , mysql = require('mysql'); 
  var xml = require('xml');

var app = express();

var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    database: 'findmybuffet'
});

connection.connect();

// all environments
app.set('port', process.env.PORT || 3007);

app.use(express.static(__dirname + '/public/images'));

app.get('/Result/',function(request,response){
    var name_of_restaurants;

    async.series( [
        // Get the first table contents
        function ( callback ) {
            connection.query('SELECT * FROM mas_buf_type', function(err, rows, fields)
                {
                        console.log('Connection result error '+err);
                        name_of_restaurants = rows;
                        callback();
                });

 }

   // Send the response
], function ( error, results ) {


 //response.json({'restaurants' : name_of_restaurants });

response.set('Content-Type', 'text/xml');
//response.send(xml(name_of_restaurants));

response.send(xml({restaurants:[
    name_of_restaurants.map(function(r){
        return [ 
            { Buf_Type_Id: r.Buf_Type_Id },
            { Buf_Type_Name: r.Buf_Type_Name },
        ]
    })
]}));


} );

} );


http.createServer(app).listen(app.get('port'),function(){
        console.log('Express server listening on port'+app.get('port'));
});

输出::

<restaurants>
    <0>
        <Buf_Type_Id>1</Buf_Type_Id>
        <Buf_Type_Name>Breakfast</Buf_Type_Name>
    </0>
</restaurants>

我们还可以清楚地看到&lt;0&gt; 正在生成......这不是必需的......如何删除它?

【问题讨论】:

    标签: xml json node.js express node-mysql


    【解决方案1】:

    所以我为您寻找了一个更好的对象到 XML 映射器。在找到我喜欢的一个之前,我尝试了三个(它易于使用,并且对您的应用程序有意义)。抛弃旧的,改用object-to-xml

    var o2x = require('object-to-xml');
    
    response.set('Content-Type', 'text/xml');
    response.send(o2x({
        '?xml version="1.0" encoding="utf-8"?' : null,
        restaurants: {
            restaurant: name_of_restaurants
        }
    }));
    

    【讨论】:

    • 感谢您的回复 [+1]....我会等待您所说的更好的答案....但我也尝试了您已经发布的那个....请参阅编辑
    • 你去!只需要找到一个更好的对象到 XML 映射器。
    【解决方案2】:

    那些 标记在那里是因为您将数组嵌入到“餐厅”中。取而代之的是,尝试将每家餐厅命名为一个对象数组:

    ...

    response.send(xml({restaurants:[
        name_of_restaurants.map(function(r){
            return { restaurant_name? :
                [ 
                    { Buf_Type_Id: r.Buf_Type_Id },
                    { Buf_Type_Name: r.Buf_Type_Name }
                ]
        })
    ]}));
    

    ...

    【讨论】:

      猜你喜欢
      • 2014-02-19
      • 2023-03-23
      • 1970-01-01
      • 2016-11-13
      • 2011-09-21
      • 2013-10-06
      • 2016-10-05
      • 2013-06-23
      • 2015-09-09
      相关资源
      最近更新 更多