-1

I am simply trying to output a single value (thumbnail) of an XML file in Node.js. I feel like I am so close but can't figure it out.

var request = require('request');
request('https://boardgamegeek.com/xmlapi/game/1', (error, response, body) => {
    if (error) { return console.log(error); }
    console.log(body.thumbnail);
});
PwrSrg
  • 55
  • 10
  • You might want to start by looking to see what `body` is. I wouldn't expect it to be an object with a `thumbnail` property. – Quentin Aug 29 '18 at 15:16
  • I was getting the XML object back correctly in body. I just can't figure out how to grab a single value (thumbnail) out of the XML object. – PwrSrg Aug 29 '18 at 16:12

2 Answers2

0

You need a XML parser, for example xml2js :

var request = require('request');
var parseString = require('xml2js').parseString;

request('https://boardgamegeek.com/xmlapi/game/1', (error, response, body) => {
    if (error) { return console.log(error); }
    parseString(body, function (err, result) {
        console.dir(result);
    });
});
boehm_s
  • 5,254
  • 4
  • 30
  • 44
  • That would make sense. Now I get a couple of arrays. Still need to figure out how to get the single XML node value. – PwrSrg Aug 29 '18 at 16:08
  • What do you call "the single XML value" ? – boehm_s Aug 29 '18 at 16:21
  • For example, there is a node in the XML file called "yearpublished" with a value of "1986". Here is what the top of the XML structure looks like: 1986 – PwrSrg Aug 29 '18 at 16:32
  • Here is the furthest I have gotten: console.dir( result.boardgames.boardgame ); The following returns 'undefined': console.dir( result.boardgames.boardgame.yearpublished ); – PwrSrg Aug 29 '18 at 16:33
  • Looks like i'm not the only one with this problem. https://stackoverflow.com/questions/20238493/xml2js-how-is-the-output – PwrSrg Aug 29 '18 at 16:45
  • Because `result.boardgames.boardgame` is an array, so if you want to retrieve "yearpublished", do the following : `result.boardgames.boardgame[0].yearpublished` – boehm_s Aug 29 '18 at 16:47
0

Double check by using the console to see all of body I.e:

      console.log(body)

Then you will see the options you have available. Show us what you get and we could be more specific or it may be enough for you to work out at a glance. You are on the right track. It just depends on the data structure that is there for you.

David White
  • 621
  • 1
  • 10
  • 23
  • Yes, I was already getting the XML object back correctly in body. I just can't figure out how to grab a single value out of the XML object. – PwrSrg Aug 29 '18 at 16:10