将json转换为xml nodejs

时间:2019-07-17 16:07:49

标签: node.js json xml

我无法使用nodejs循环将简单的json结构转换为xml。我有工作的代码,但是它不使用循环。每当我使用循环时,它都会中断并仅打印出数组中的最后一项。任何帮助都很棒

这是没有循环的代码

$sql = "
SELECT * 
  FROM chambre 
 WHERE id NOT IN 
    (SELECT id_chambre 
       FROM reservation_client 
     WHERE end < '2019-07-20'  
        AND start > '2019-07-19 '
    )";

这是我的代码,循环中断

var express = require('express');
var app = express();
var port = process.env.PORT || 80;
var builder = require('xmlbuilder');
const https = require("https");

var parkingData;
var levelData;

app.get('/',function(req,res) {
  //const url = 'https://insights.parkassist.com/sites/santana-row/status/v2/zones.json';
  const url = 'https://api-prismview:S8EPh8xaStaXApum@api.parkassist.com/santana-row/v2/zones.json';
  https.get(url, httpsRes => {
    httpsRes.setEncoding("utf8");
    let body = "";
    httpsRes.on("data", data => {
      body += data;
    });
    httpsRes.on("end", function() {
      bodyJSON = JSON.parse(body);
      parkingData = bodyJSON;
      levelData = builder.create('levels', { encoding: 'utf-8' })
        .ele('level')
          .ele('id', parkingData[0].id).up()
          .ele('name', parkingData[0].name).up()
          .ele('counts')
            .ele('available', parkingData[0].counts.available).up()
            .ele('occupied', parkingData[0].counts.occupied).up()
            .ele('out_of_service', parkingData[0].counts.out_of_service).up()
            .ele('reserved', parkingData[0].counts.reserved).up()
            .ele('timestamp', parkingData[0].counts.timestamp).up()
            .ele('total', parkingData[0].counts.total).up()
            .ele('vacant', parkingData[0].counts.vacant).up()
          .up()
        .up()
        .ele('level')
          .ele('id', parkingData[1].id).up()
          .ele('name', parkingData[1].name).up()
          .ele('counts')
            .ele('available', parkingData[1].counts.available).up()
            .ele('occupied', parkingData[1].counts.occupied).up()
            .ele('out_of_service', parkingData[1].counts.out_of_service).up()
            .ele('reserved', parkingData[1].counts.reserved).up()
            .ele('timestamp', parkingData[1].counts.timestamp).up()
            .ele('total', parkingData[1].counts.total).up()
            .ele('vacant', parkingData[1].counts.vacant).up()
          .up()
        .up()
        .ele('level')
          .ele('id', parkingData[2].id).up()
          .ele('name', parkingData[2].name).up()
          .ele('counts')
            .ele('available', parkingData[2].counts.available).up()
            .ele('occupied', parkingData[2].counts.occupied).up()
            .ele('out_of_service', parkingData[2].counts.out_of_service).up()
            .ele('reserved', parkingData[2].counts.reserved).up()
            .ele('timestamp', parkingData[2].counts.timestamp).up()
            .ele('total', parkingData[2].counts.total).up()
            .ele('vacant', parkingData[2].counts.vacant).up()
          .up()
        .up()
        .ele('level')
          .ele('id', parkingData[3].id).up()
          .ele('name', parkingData[3].name).up()
          .ele('counts')
            .ele('available', parkingData[3].counts.available).up()
            .ele('occupied', parkingData[3].counts.occupied).up()
            .ele('out_of_service', parkingData[3].counts.out_of_service).up()
            .ele('reserved', parkingData[3].counts.reserved).up()
            .ele('timestamp', parkingData[3].counts.timestamp).up()
            .ele('total', parkingData[3].counts.total).up()
            .ele('vacant', parkingData[3].counts.vacant).up()
          .up()
        .up()
        .end();
        res.set('Content-Type', 'text/xml');
        res.send(levelData);
    }.bind(this));
  });

});

0 个答案:

没有答案