我完全是 node.js 的菜鸟,我是一名大学研究员。我有很多 XML 和 JSON 格式的文件,我有很多文件夹和文件,我需要阅读它并创建一个标准(在 JSON/CSV 中),最后将它加载到 MySQL 数据库中,有什么提示吗?你知道它的 npm 包还是完整的解决方案?
有转换XML为JSON像很多包xmljs
,xml2json
等等。
但是看起来您还需要转换为标准格式才能插入数据库。
我自己也有这个问题,为此我写了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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句