I am trying to use jquery on node.js under Bluemix but it is not working. I am retrieving an xml data from an URL and then try to parse it using jquery however I get the following error:
ERR /home/vcap/app/MyNService.js:57
ERR var $doc = $.parseXML(xml);
ERR ^
ERR TypeError: undefined is not a function
This my code:
/*eslint-env node*/
//------------------------------------------------------------------------
// node.js starter application for Bluemix
//----------------------------------------------------------------------
// This application uses express as its web server
// for more info, see: http://expressjs.com
var express = require('express');
// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');
// create a new express server
var app = express();
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));
// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
// start server on the specified port and binding host
app.listen(appEnv.port, function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});
var http = require('http');
var $ = require('jquery');
var XMLSerializer = new (require('xmldom').XMLSerializer)();
var username = 'xxxxx';
var password = 'xxxxx';
var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');
var options = {
host: 'www-945.com',
path: '/xxx/xxx/myfeed/csnapi.wss?csnapi.requid=getuser&csnapi.subsonly=1& csnapi.maxsubs=1&csnapi.maxmsgs=1&csnapi.uid=sssss',
headers: {'Authorization': auth
}
};
var req = http.request(options,function(res){
var xml = '';
res.on('data', function(chunk){
xml += chunk;
console.log(xml);
var $doc = $.parseXML(xml);
$(doc).find('User').attr('UID');
console.log(XMLSerializer.serializeToString(doc));
});
});
req.on('error', function(err){
console.log('problem with request: ' + err.message);
});
req.end();
Thanks
I tried using xml2js and when the xml is converted to json I get this output
{ User:
{ '$': { UID: '1111111' },
Sub:
[ { '$': { SName: 'Component ID DDDFORLUW' },
SID: [ 'J14767abf217' ],
FID: [ 'J14767abf20a' ],
SubKey: [ 'apar' ],
FName: [ 'APARs' ],
Detail:
[ { '$': { name: 'DOCTYPE', value: 'component' } },
{ '$': { name: 'DeliverFolder', value: 'YES' } },
{ '$':
{ name: 'categorytitle',
value: 'COMPONENT ID - DDDFORLUW - DDDDDUW' } },
{ '$': { name: 'categoryid', value: 'DDDFORLUW' } },
{ '$': { name: 'DeliverRSS', value: 'YES' } },
{ '$': { name: 'EmailSending', value: 'YES' } } ] } ] } }