简体   繁体   English

Node.js - 获取单个 XML 值

[英]Node.js - Get a single XML value

I am simply trying to output a single value (thumbnail) of an XML file in Node.js.我只是想在 Node.js 中输出 XML 文件的单个值(缩略图)。 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);
});

You need a XML parser, for example xml2js :您需要一个 XML 解析器,例如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);
    });
});

Double check by using the console to see all of body Ie:使用控制台仔细检查以查看所有主体,即:

      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.这仅取决于适合您的数据结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM