【问题标题】:read a lot of file and xml to json in node.js在node.js中读取大量文件和xml到json
【发布时间】:2018-05-08 12:20:12
【问题描述】:

我在 node.js 中完全是菜鸟,我是一名大学研究员。 我有很多 XML 和 JSON 格式的文件,我有很多文件夹和文件,我需要阅读并创建一个标准(在 JSON/CSV 中),最后将其加载到 MySQL 数据库中,有什么提示吗?你知道它的 npm 包或完整的解决方案吗?

【问题讨论】:

    标签: json node.js xml file converter


    【解决方案1】:

    谢谢@tuananh 是否可以将标签的属性用作键或值?我在 attr 中拥有的一些数据值,例如 <name type="sub-name">Hotel name</name><hotelName id='12345'>Hotel name </name> 可以在 camaro 中执行吗?

    我没有像<name type="sub-name">Hotel <nameHotel>Hilton</nameHotel></name>这样的纯数据可以删除标签nameHotel并将其另存为{name: 'Hotel Hilton'} 非常感谢

    【讨论】:

    • 目前还不能在key中使用xpath,以后会考虑的。
    【解决方案2】:

    有许多用于将 xml 转换为 json 的包,例如 xmljsxml2json 等。

    但看起来您也需要转换为标准格式才能插入数据库。

    我自己也有这个问题,为此我写了camaro:convert and transform xml to json

    我所要做的就是编写一个输出模板,我希望我的 xml 也可以使用下面的 xpath 语法进行转换

    当然,您可以将所需的所有属性展平;以下只是 camaro 可以做什么的示例。

    const transform = require('camaro')
    const fs = require('fs')
    
    const xml = fs.readFileSync('examples/ean.xml', 'utf-8')
    const template = {
        cache_key: '/HotelListResponse/cacheKey',
        hotels: ['//HotelSummary', {
            hotel_id: 'hotelId',
            name: 'name',
            rooms: ['RoomRateDetailsList/RoomRateDetails', {
                rates: ['RateInfos/RateInfo', {
                    currency: 'ChargeableRateInfo/@currencyCode',
                    non_refundable: 'boolean(nonRefundable = "true")',
                    price: 'number(ChargeableRateInfo/@total)'
                }],
                room_name: 'roomDescription',
                room_type_id: 'roomTypeCode'
            }]
        }],
        session_id: '/HotelListResponse/customerSessionId'
    }
    
    const result = transform(xml, template)
    

    【讨论】:

      猜你喜欢
      • 2015-12-28
      • 2017-05-29
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多