【问题标题】:Extracting info from nodejs xml2js 'result'从nodejs xml2js'结果'中提取信息
【发布时间】:2020-07-01 11:16:32
【问题描述】:

使用 xml2js nodejs 模块,我已经成功地使用以下代码从(大!)xml 文档中提取;

var fs = require('fs'),
    parseString = require('xml2js').parseString,
    request = require('request');
    request('https://fb.motorcyclenews.com/mcn-bfs-fb-marketplace.xml', function (error, response, body) {
       if (!error && response.statusCode == 200) {
          parseString(body, function (err, result) {
            console.log(JSON.stringify(result));
          });
     }});

这个“有效”,结果的一小部分摘录如下;

"dealer_id":["--"],"dealer_name":["-- 摩托车"],"vehicle_registration_plate":["--"],"fb_page_id":["--"],"dealer_communication_channel":["LEAD_FORM"],"dealer_phone":["+44 --"],"dealer_privacy_policy_url":["https://www.motorcyclenews.com/privacy-policy/"],"mileage":[{"value":["552"],"unit":[" MI"]}],"address":[{"$":{"format":"simple"},"component":[{"":"Ivy 路车库","$":{"name":"addr1"}},{"":"Ivy 路","$":{"name":"city"}},{"":"Lancashire","$":{"name":"region"}},{" ":"UK","$":{"name":"country"}},{"":"-- 6DJ","$":{"name":"postal_code"}}]}],"vehicle_type":["MOTORCYCLE"],"dealer_url":["https://www.motorcyclenews.com/api/bfs -dealer/redirect/--"]},{"vehicle_id":["56968134"],"make":["HONDA"],"model":["NSS300 FORZA"],"year":["2018"],"transmission":["OTHER"],"body_style":["OTHER"],"fuel_type":["GASOLINE"],"drivetrain":[" RWD"],"描述":["本田 FORZA 300CC ULTRA LOW MILES,配备顶盒,护手,加热把手, 完好无损,为什么要坐火车或公共汽车节省时间和金钱,价格低 成本融资全国交付 可用。"],"图片":[{"url":["https://cdn.images.autoexposure.co.uk/AETA86362/AETV24605587_5.jpg?t=--"]}],"url": [“https://www.motorcyclenews.com/bikes-for-sale/honda/nss300-forza/--/”],“标题”:[“本田 NSS300 FORZA (2018)"],"价格":["3,750.00 GBP"],"state_of_vehicle":["USED"],"exterior_color":["BLACK"],"latitude":["51.7241967327"],"longitude":["0.4833483158"],"dealer_id":[" -- City Motorcycles LTD"],"dealer_name":["-- City Motorcycles LTD"],"vehicle_registration_plate":["--"],"fb_page_id":["--"],"dealer_communication_channel":["LEAD_FORM"],"dealer_phone":["+44 --"],"dealer_privacy_policy_url":["https://www.motorcyclenews.com/privacy-policy/"],"mileage":[{"value":["1793"],"unit":[" MI"]}],"地址":[{"$":{"format":"simple"},"component":[{"":"198-200 巴多 路","$":{"name":"addr1"}},{"":"未知","$":{"name":"city"}},{" ":"埃塞克斯","$":{"name":"region"}},{"":"UK","$":{"name":"country"}},{"":"CM2 9QP","$":{"name":"postal_code"}}]}],"vehicle_type":["MOTORCYCLE"],"dealer_url":["https://www.motorcyclenews.com/api/bfs -dealer/redirect/--"]},{"vehicle_id":["56970343"],"make":["HONDA"],"model":["VISION 110"],"year":["2011"],"transmission":["OTHER"],"body_style":["OTHER"],"fuel_type":["GASOLINE"],"drivetrain":[" RWD"],"description":[""],"url":["https://www.motorcyclenews.com/bikes-for-sale/honda/vision-110/--

/"],"title":["本田

VISION 110 (2011)"],"price":["1,250.00 GBP"],"state_of_vehicle":["USED"],"exterior_color":["Red"],"latitude":["52.2398688251"],"longitude":["0.752311123"],"dealer_id":["约翰 班克斯本田摩托车"],"dealer_name":["约翰 -- 本田 摩托车"],"vehicle_registration_plate":["--"],"fb_page_id":["7714743854"],"dealer_communication_channel":["LEAD_FORM"],"dealer_phone":["+44 --"],"dealer_privacy_policy_url":["https://www.motorcyclenews.com/privacy-policy/"],"mileage":[{"value":["5051"],"unit":[" MI"]}],"address":[{"$":{"format":"simple"},"component":[{"":"Kempson 方式","$":{"name":"addr1"}},{"":"Moreton 方式","$":{"name":"city"}},{"":"Suffolk","$":{"name":"region"}},{" ":"UK","$":{"name":"country"}},{"":"-- 7AR","$":{"name":"postal_code"}}]}],"vehicle_type":["MOTORCYCLE"],"dealer_url":["https://www.motorcyclenews.com/api/bfs -dealer/redirect/--"]},{"vehicle_id":["--"],"make":["HONDA"],"model":["CRF250"],"year":["2019 "],"transmission":["OTHER"],"body_style":["OTHER"],"fuel_type":["GASOLINE"],"drivetrain":["RWD"],"description":["" ],"url":["https://www.motorcyclenews.com/bikes-for-sale/honda/crf250/--/"],"title":["HONDA CRF250 (2019)"],"价格":["4,500.00 GBP"],"state_of_vehicle":["USED"],"exterior_color":["Red"],"latitude":["52.2398688251"],"longitude":["0.752311123"],"dealer_id":["约翰 班克斯本田摩托车"],"dealer_name":["约翰 -- 本田 摩托车"],"vehicle_registration_plate":["--"],"fb_page_id":["7714743854"],"dealer_communication_channel":["LEAD_FORM"],"dealer_phone":["+44 --"],"dealer_privacy_policy_url":["https://www.motorcyclenews.com/privacy-policy/"],"mileage":[{"value":["1000"],"unit":[" MI"]}],"address":[{"$":{"format":"simple"},"component":[{"":"Kempson 方式","$":{"name":"addr1"}},{"":"Moreton 方式","$":{"name":"city"}},{"":"Suffolk","$":{"name":"region"}},{" ":"UK","$":{"name":"country"}},{"":"-- 7AR","$":{"name":"postal_code"}}]}],"vehicle_type":["MOTORCYCLE"],"dealer_url":["https://www.motorcyclenews.com/api/bfs -dealer/redirect/--"]}]}}

但我现在想做的是从中提取一些信息。

我特别想知道列表中出现了多少次,例如“vehicle_id”的条目。

然后我可以将它用于我的测试的其他部分。

任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: javascript xml2js


    【解决方案1】:

    假设你发布的输出是一个对象,试试这个

    let finalResult = Object.keys(result).reduce( (allNames, name) => { 
      if (name in allNames && name === "vehicle_id") {
        allNames[name]++
      }
      else if(name === "vehicle_id") {
        allNames[name] = 1
      }
      return allNames
    }, {})
    
    console.log(finalResult)
    

    【讨论】:

    • 感谢@Ravi 的回复但是,当我将此代码添加到我现有的代码脚本中时,无论我在哪里添加console.log(allNames),都不会向终端输出任何内容。我对console.log 有误解吗?非常感谢
    • @DarrenHarley 您必须将 allNames 放入变量中。我已经编辑了答案。再检查一遍。
    • 再次感谢您的帮助,但是使用您修改后的代码,我在运行测试时仍然没有显示任何内容。谢谢 :(
    • @DarrenHarley 你能分享一下输入吗?我很乐意检查和编辑代码。
    猜你喜欢
    • 1970-01-01
    • 2014-06-25
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 2019-03-27
    • 2017-06-07
    相关资源
    最近更新 更多