title: Node JS body-parser-xml Not Working
body: <p>I want to handle both JSON- and XML-type requests, so I am using body-parser-xml in my node application. </p>
  
  <p>My problem is the second XML element is not binding to <code>req.body</code>, but I get the first element value instead.</p>
  
  <p>My code is:</p>
  
  <pre><code>var loopback = require('loopback');
  var boot = require('loopback-boot');
  var cfenv = require('cfenv');
  var bodyParser = require("body-parser");
  var cookieParser = require('cookie-parser');
  require('body-parser-xml')(bodyParser);
  
  var app = module.exports = loopback();
  var appEnv = cfenv.getAppEnv();
  
  app.use(bodyParser.json());
  app.use(bodyParser.xml({
      limit: '1MB', // Reject payload bigger than 1 MB 
      xmlParseOptions: {
          normalize: true, // Trim whitespace inside text nodes 
          normalizeTags: false, // Transform tags to lowercase 
          explicitArray: false // Only put nodes in array if &gt;1 
      }
  }));
  
  app.use(bodyParser.urlencoded({
      "extended": true
  }));
  
  // boot scripts mount components like REST API
  boot(app, __dirname);
  
  app.start = function() {
      // start the web server
      return app.listen(process.env.PORT || 3000, function() {
          console.log("env port" + process.env.PORT);
          app.emit('started');
          var baseUrl = app.get('url').replace(/\/$/, '');
          console.log('Web server listening at: %s', baseUrl);
          if (app.get('loopback-component-explorer')) {
              var explorerPath = app.get('loopback-component-explorer').mountPath;
              console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
          }
      });
  };
  
  // start the server if `$ node server.js`
  if (require.main === module) {
      app.start();
  }
  </code></pre>
  
  <p>My Routes:</p>
  
  <pre><code>module.exports = function(app) {
      var router = app.loopback.Router();
      var User = app.models.pusers;
      var js2xmlparser = require("js2xmlparser");
  
      app.use(function(req, res, next) {
          res.header("Access-Control-Allow-Origin", "*");
          res.header("Access-Control-Allow-Headers", "X-Requested-With");
          if (req.get("content-type") !== 'undefined') {
              if (req.get("content-type") == 'application/json') {
                  res.setHeader('content-type', req.get("content-type"));
              } else if (req.get("content-type") == 'application/xml') {
                  res.setHeader('content-type', req.get("content-type"));
              }
          }
          next();
      });
  
      app.middleware('initial', function logResponse(req, res, next) {
          res.on('finish', function() {});
          req.on('end', function(data) {});
          req.on('data', function(data) {
              // the request was handled, print the log entry
              console.log(req.method, req.originalUrl, res.statusCode);
              if (req.get("content-type") == 'application/xml') {
                  console.log("xml data's :" + data);
              } else if (req.get("content-type") == 'application/json') {
                  console.log("json data's :" + data);
              }
          });
          next();
      });
  
      function responseHandler(req, res, data) {
          if (req.get("content-type") == 'application/json') {
              return JSON.stringify(data);
          } else if (req.get("content-type") == 'application/xml') {
              return js2xmlparser("response", JSON.stringify(data));
          } else
              return data;
      }
  
      router.post('/login', function(req, res) {
          var response = {};
          console.log(req.body);
          console.log(req.body.username);
          try {
              User.find({
                  where: {
                      name: req.body.username
                  }
              }, function(err, data) {
                  if (err) {
                      response = {
                          "error": true,
                          "message": "Error fetching data"
                      };
                  } else {
                      if (data.length != 0) {
                          if (data[0].name == req.body.username &amp;&amp; data[0].password == req.body.password) {
                              response = {
                                  "error": false,
                                  "data": "Success"
                              };
                          } else {
                              response = {
                                  "error": false,
                                  "data": "Password is incorrect"
                              };
                          }
                      } else if (data.length == 0) {
                          response = {
                              "error": false,
                              "data": "Username is incorrect"
                          };
                      }
                  }
                  console.log("Check login");
                  res.end(responseHandler(req, res, response));
              });
          } catch (ex) {
              console.error("Error while retrive all data from User Model by name", ex);
              res.end(responseHandler(req, res, "Error while inserting User Model by name"));
          }
      });
  
      app.use(router);
  }
  </code></pre>
  
  <p>How can i solve this problem?</p>