Mjuice
Mjuice

Reputation: 1288

How do I make use of an xml file in node.js?

In my server.js file, I make an HTTP GET request that is suppose to return xml. When I log the GET request's response to the console, it is gibberish containing lots of question marks and black diamonds as you can see in the photo below:enter image description here

When I take the same url that I'm using in my GET request and I open it in the browser it automatically downloads a gzip file, which after it's unzipped contains a legible xml file with the data (inside my text editor).

How do I get the xml in its correct form inside my server.js file? I need to make use of it in my program, not inside a text editor (obviously).

Here is my GET request:

axios.get('http://www2.jobs2careers.com/feed.php?id=1237-2595&c=1&pass=HeahE0W1ecAkkF0l')
  .then(function(response) {
    console.log(response.data);
  });

I've tried to extract the gzip file using the targz library as shown below:

axios.get('http://www2.jobs2careers.com/feed.php?id=1237-2595&c=1&pass=HeahE0W1ecAkkF0l')
  .then(function(response) {
    targz().extract(response.data, '/data', function(err){
      if (err) {
        console.log('Something is wrong ', err.stack);
      }
      console.log('Job done!');
    });
  });

I get an error in the console saying : "Path must be a string without null bytes". Should I be using the extract method from targz or am I just using it incorrectly? I'm trying to "extract" or unzip the response.data.

Upvotes: 0

Views: 1532

Answers (2)

user5734311
user5734311

Reputation:

Based on this: Simplest way to download and unzip files in Node.js cross-platform?

var feedURL = 'http://www2.jobs2careers.com/feed.php?id=1237-2595&c=1&pass=HeahE0W1ecAkkF0l';

var request = require('request'),
    zlib = require('zlib'),
    fs = require('fs'),
    out = fs.createWriteStream('./feed.xml');

request(feedURL).pipe(zlib.createGunzip()).pipe(out);

Upvotes: 2

Brandon R Him
Brandon R Him

Reputation: 161

From the updated code, it appears you that the first parameter (response.data) needs to be set to a path on the filesystem of a gzip file, hence the null byte error. I would consider writing to the file system, then extract, or another module that would let you extract from a url.

When you do get the XML out of the extracted gzip file (which you are on the right path, no pun intended), you can use a node module such as xml2js, which will parse the xml into a Javascript object, and makes it quite easy to work with.

Upvotes: 0

Related Questions