$ npm install camaro
camaro is an utility to transform XML to JSON, using Node.js binding to native XML parser pugixml, one of the fastest XML parser around.
transform() is now an async function.const { transform } = require('camaro')toJson() function to convert the whole XML input.prettyPrint() to pretty print XML.camaro x 362 ops/sec ±0.31% (87 runs sampled)
rapidx2j x 226 ops/sec ±0.27% (88 runs sampled)
xml2json x 46.32 ops/sec ±1.39% (61 runs sampled)
xml2js x 50.51 ops/sec ±7.22% (68 runs sampled)
fast-xml-parser x 256 ops/sec ±0.63% (86 runs sampled)
xml-js x 45.05 ops/sec ±6.19% (61 runs sampled)
Please note that this is an unfair game for camaro because it only transform those fields specified in template. The whole reason of me creating this is because most of the time, I'm just interested in some of the data in the whole XML mess.
Benchmark run on MacBookPro14,1 - Intel Core i5 CPU @ 2.30GHz using Node v8.10.0.
I may expose another method to transform the whole XML tree so that the benchmark will better reflect the real performance.
For complete benchmark, see benchmark/index.md.
yarn add camaro
# npm install camaro
You can use our custom template format powered by XPath.
We also introduce some custom syntax such as:
#, that means it's a constant. E.g: #1234 will return 1234lower-case, upper-case, title-case, camel-case, snake-case and string-join. Eventually, I'm hoping to add all XPath 2.0 functions but these are all that I need for now. PRs welcome.The rest are pretty much vanilla XPath 1.0.
For complete API documentation, please see API.md
const { ready, transform, prettyPrint } = 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'
}
;(async function () {
await ready()
const result = await transform(xml, template)
console.log(result)
const prettyStr = await prettyPrint(xml, { indentSize: 4})
console.log(prettyStr)
})()
© 2010 - cnpmjs.org x YWFE | Home | YWFE