0

I am trying to read an hosted xml file from server but it returns as garbage value. Below is my code:-

var url = 'http://www.tipgin.net/example-data/livescore-feed-example.xml';
    var req = http.get(url, function(response) {
      // save the data
      var xml = '';
     res.set('Content-Type', 'text/html');
     response.on('data', function(chunk) {
        xml += chunk;
        res.write(chunk);
      });

      response.on('end', function() {
        // parse xml

            res.write(xml);
            res.end();
      });

      // or you can pipe the data to a parser
      //res.pipe(dest);
    }).on('error', function(err) {
      // debug error
    });

In the response it prints out as below:-

��]�r۸�}��`�aj�!n\x�t���Ĺ'>q�s�tMu�$1�HP���������y�o�� �"!/�s^Z����7�?���e��pQ�E���#���b����G�z�$>��O?f� /'��^���5�>="�1"����䄢y����Ge1�pq�����+�M�*_���G,�s��#/gK���T=���7�\��L��ӣs����K�L  �CWW��oG?��d���cٚ��~�B  ���~�����(�B9l�\�|��2��嫘o���1���(�l�N���G��(�|��H�AUl� .....

I have tried other XML service and that works fine. This issue persist only with this url when I try to access the file. Is there anything additional I have to add into the code here?

Villie
  • 261
  • 3
  • 18
  • Well which encoding does the XML you are trying to read have, does it declare it correctly in its XML declaration, does the server send the right HTTP header? What happens when you load the URL into a browser window, does the browser display the XML contents correctly? If so, which encoding does it indicate for the document? – Martin Honnen Feb 24 '15 at 13:08
  • I am not sure which encoding it is as the xml is a part of third party service which I am calling from the code. I am able to read the XML when I write a php curl code. I am new to nodejs so not sure what I am missing here. – Villie Feb 24 '15 at 13:39
  • Also is displaying correctly in browser aswell. – Villie Feb 24 '15 at 14:01

1 Answers1

0

I was having the same issue, until I realized the file I was getting was zipped. In this particular case I could request it unzipped from the server and that solved my issue. If you can't do that, then you might wanna try to unzip it before reading it. There are a few suggestions in this SO entry about how to do that.

Hope this helps!

Community
  • 1
  • 1
Asinus Rex
  • 535
  • 9
  • 29