Skip to content

Instantly share code, notes, and snippets.

@jasonrhodes
Created October 11, 2013 21:04
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jasonrhodes/6942015 to your computer and use it in GitHub Desktop.
Trying to read XML from a search API and convert it to JSON.
var http = require('http');
var xmld = require('xml2js');
var express = require('express');
var app = express();
// app.use(express.static(__dirname + '/public'));
app.get("/", function (req, res) {
var options = {
hostname: "search.mysite.com",
path: '/search?site=hub&client=hub_frontend&output=xml_no_dtd&q=cats'
};
var gsaReq = http.get(options, function (response) {
response.on('data', function (chunk) {
console.log(chunk);
// xmld.parseString(chunk, function (err, result) {
// if (err) console.log("ERROR", err);
// console.log('my resultsult', result);
// });
});
}).on('error', function (e) {
console.log('problem with request: ' + e.message);
});
});
app.listen(3000);
@jasonrhodes
Copy link
Author

Sometimes line 17 prints out what looks like an XML string (what I expect) but sometimes it prints out lines like this:

<Buffer 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 22 31 2e 30 22 20 65 6e 63 6f 64 69 6e 67 3d 22 49 53 4f 2d 38 38 35 39 2d 31 22 20 73 74 61 6e 64 61 6c 6f 6e ...>
<Buffer 20 7c 20 48 75 62 3c 2f 54 3e 3c 52 4b 3e 35 3c 2f 52 4b 3e 3c 45 4e 54 5f 53 4f 55 52 43 45 3e 54 33 2d 51 45 52 45 56 55 54 33 38 57 53 51 5a 3c 2f 45 ...>

I'm not sure how to receive the data from the request, that should be an XML string, and make sure I have that string so I can convert it into JSON and push it into a template... help?

@psi-4ward
Copy link

   var gsaReq = http.get(options, function (response) {
var xmlstr = '';
        response.on('data', function (chunk) {
            xmlstr += chunk.toString();
            // xmld.parseString(chunk, function (err, result) {

            //     if (err) console.log("ERROR", err);

            //     console.log('my resultsult', result);

            // });
        });
    }).on('error', function (e) {
        console.log('problem with request: ' + e.message);
    }).on('end', function() { console.log(xmlstr);}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment