0
/**
 * 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);

Context: I am trying to read xml data from a server, pull the data from the elements I want, and then write a new xml. I have these processes figured out independently, but when i try to pass my 'data' object to my xmlbuilder it acts like it doesn't receive. I've tried a couple different ways of trying to refer to my 'data' object and I can't figure out how to do it outside of the parseHandler class. I am new to JS(obvious)? at this point im just trying to figure out how to access the my data object contents. Thanks in advance....

somedude
  • 33
  • 6

0 Answers0