title: Expressjs response as JSON and Xml
body: <p>What i am doing:: I am trying to generate json and xml output for a dataset from database</p>
  
  <p><strong>Express Code</strong>:: Here i am trying for JSON response </p>
  
  <pre><code>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'));
  });
  </code></pre>
  
  <p><strong>My Output</strong>::</p>
  
  <pre><code>{
      "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"
          }
      ]
  }
  </code></pre>
  
  <p><strong>Now instead of</strong> </p>
  
  <pre><code> response.json({'restaurants' : name_of_restaurants });
  </code></pre>
  
  <p><strong>I have added these lines to get <code>XML</code> output</strong></p>
  
  <pre><code>response.set('Content-Type', 'text/xml');
  response.send(xml(name_of_restaurants));
  </code></pre>
  
  <p><strong>Output</strong>::</p>
  
  <pre><code>&lt;Buf_Type_Id&gt;1&lt;/Buf_Type_Id&gt;
  &lt;Buf_Type_Id&gt;2&lt;/Buf_Type_Id&gt;
  &lt;Buf_Type_Id&gt;3&lt;/Buf_Type_Id&gt;
  </code></pre>
  
  <p><strong>My QUESTION</strong>:: </p>
  
  <ul>
  <li>Clearly you can see both output are different and i cannot find
  second column in xml output</li>
  <li>how to resolve this</li>
  <li>what changes should i need to make</li>
  </ul>
  
  <h1>{EDIT}</h1>
  
  <pre><code>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'));
  });
  </code></pre>
  
  <p><strong>Output</strong>::</p>
  
  <pre><code>&lt;restaurants&gt;
      &lt;0&gt;
          &lt;Buf_Type_Id&gt;1&lt;/Buf_Type_Id&gt;
          &lt;Buf_Type_Name&gt;Breakfast&lt;/Buf_Type_Name&gt;
      &lt;/0&gt;
  &lt;/restaurants&gt;
  </code></pre>
  
  <p>Also clearly we can see <code>&lt;0&gt;</code> getting generated .... which is not required ...how to remove this ?</p>