/**
* Created by Andy on 3/6/14.
*/
var xml2js = require('xml2js');
var fs = require('fs');
var http = require('http');
var parseString = require('xml2js').parseString;
var resultDoc = {};
var options = {
host: '130.207.32.75',
path: '/xmlfeed.rb'
};
var callback = function (response) {
var responseMessage = '';
response.on('data', function (chunk) {
responseMessage += chunk;
});
response.on('end', function () {
console.log("Done")
console.log(responseMessage)
parseString(responseMessage, function (err, result) {
this.resultDoc = result;
parseHandler();
});
});
var parseHandler = function () {
var temp1 = this.resultDoc.currentConditions.ports[0].port[0].condition[0].currentReading[0]
var temp2 = this.resultDoc.currentConditions.ports[0].port[1].condition[0].currentReading[0]
var temp3 = this.resultDoc.currentConditions.ports[0].port[2].condition[0].currentReading[0]
var temp4 = this.resultDoc.currentConditions.ports[0].port[3].condition[0].currentReading[0]
var humidity = this.resultDoc.currentConditions.ports[0].port[1].condition[1].currentReading[0]
var data = {TEMP1:temp1,TEMP2:temp2,TEMP3:temp3,TEMP4:temp4,HUMIDITY:humidity};
return data;
};
};
http.request(options, callback).end();
var builder = new xml2js.Builder();
//console.log('Is the temp array below????');
console.log(callback.parseHandler);
//console.log('----------------');
//var xml = builder.buildObject(data);
//console.log(xml);
上下文:我正在尝试从服务器读取xml数据,从我想要的元素中提取数据,然后编写一个新的xml。我已经独立地想出了这些过程,但是当我尝试传递我的数据时#39;对象我的xmlbuilder就像它没有收到的那样。我尝试了几种不同的方法来尝试引用我的数据'对象,我无法弄清楚如何在parseHandler类之外完成它。我是JS的新手(很明显)?此时我只想弄清楚如何访问我的数据对象内容。提前谢谢....