Skip to content

Latest commit

 

History

History
81 lines (63 loc) · 1.98 KB

API.md

File metadata and controls

81 lines (63 loc) · 1.98 KB

6.0.0 API Reference

camaro

transform(xml, template)

Transform xml string to JSON using the given template powered by XPath where:

  • xml - the input xml string
  • template - the XPath template object, powered by XPath.
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'
}

;(async function () {
    const result = await transform(xml, template)
    console.log(result)
})()

toJson(xml)

Not yet implemented

Transform xml string to JSON where:

  • xml - the input xml string
const { toJson } = require('camaro')
const fs = require('fs')

const xml = fs.readFileSync('examples/ean.xml', 'utf-8')

;(async function () {
    const result = await toJson(xml)
    console.log(result)
})()

prettyPrint(xml)

Pretty print xml string where:

  • xml - the input xml string
  • options - an optional object with the following optional keys:
    • indentSize - a number of space to use for indenting
const { prettyPrint } = require('camaro')
const fs = require('fs')

const xml = fs.readFileSync('examples/ean.xml', 'utf-8')

;(async function () {
    const result = await prettyPrint(xml, { indentSize: 4 })
    console.log(result)
})()