camaro
Transforming XML to JSON using Node.js binding to native pugixml parser library
Last updated 7 years ago by tuananh .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install camaro 
SYNC missed versions from official npm registry.

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.

npm npm bundle size Build Status Travis AppVeyor TypeScript definitions on DefinitelyTyped npm

Demo on CodeSandbox.io

🤘 Features

  • Transform XML to JSON.

    • Only take properties that you're interested in.
    • Output is a ready to use JS object.
  • Written in C++ and compiled down to WebAssembly so no re-compilation needed.

    • No need to build binary whenever a new Node version released.
    • Work on all major platforms (OS X, Linux and Windows and the web). See Travis CI and AppVeyor build status for details.
    • AWS Lambda friendly (or serverless in general).
  • It's pretty fast on large XML strings.

    • We're using pugixml underneath. It's one of the fastest XML parser around.
    • Scale well with multi-core processor by use of worker_threads pool (Node >= 12).
  • Pretty print XML.

🔥 Benchmark

camaro v6: 1,395.6 ops/sec
fast-xml-parser: 153 ops/sec
xml2js: 47.6 ops/sec
xml-js: 51 ops/sec
300 KB XML file 100 KB XML file
60 KB XML file 7 KB XML file

XML file is an actual XML response from Expedia API. I just delete some nodes to change its size for benchmarking.

For complete benchmark, see benchmark/index.md.

  • 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.
  • I may expose another method to transform the whole XML tree so that the benchmark will better reflect the real performance.
  • 🚧 Performance on small XML strings will probably be worse than pure JavaScript implementation. If your use cases consist of small XML strings only, you probably don't need this.
  • Some other libraries that I used to use for benchmark like rapidx2j or xml2json no longer works on Node 14 so I remove them from the benchmark.

intro

Installation

yarn add camaro
# npm install camaro

Usage

You can use our custom template format powered by XPath.

We also introduce some custom syntax such as:

  • if a path start with #, that means it's a constant. E.g: #1234 will return 1234
  • if a path is empty, return blank
  • Some string manipulation functions which are not availble in XPath 1.0 such as lower-case, upper-case, title-case, camel-case, snake-case, string-join or raw. 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

Additional examples can be found in the examples folder at https://github.com/tuananh/camaro/tree/develop/examples.

const { transform, prettyPrint } = require('camaro')

const xml = `
    <players>
        <player jerseyNumber="10">
            <name>wayne rooney</name>
            <isRetired>false</isRetired>
            <yearOfBirth>1985</yearOfBirth>
        </player>
        <player jerseyNumber="7">
            <name>cristiano ronaldo</name>
            <isRetired>false</isRetired>
            <yearOfBirth>1985</yearOfBirth>
        </player>
        <player jerseyNumber="7">
            <name>eric cantona</name>
            <isRetired>true</isRetired>
            <yearOfBirth>1966</yearOfBirth>
        </player>
    </players>
`

/**
 * the template can be an object or an array depends on what output you want the XML to be transformed to.
 * 
 * ['players/player', {name, ...}] means that: Get all the nodes with this XPath expression `players/player`.
 *      - the first param is the XPath path to get all the XML nodes.
 *      - the second param is a string or an object that describe the shape of the array element and how to get it.
 * 
 * For each of those XML node
 *      - call the XPath function `title-case` on field `name` and assign it to `name` field of the output.
 *      - get the attribute `jerseyNumber` from XML node player
 *      - get the `yearOfBirth` attribute from `yearOfBirth` and cast it to number.
 *      - cast `isRetired` to true if its string value equals to "true", and false otherwise.
 */

const template = ['players/player', {
    name: 'title-case(name)',
    jerseyNumber: '@jerseyNumber',
    yearOfBirth: 'number(yearOfBirth)',
    isRetired: 'boolean(isRetired = "true")'
}]

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

    const prettyStr = await prettyPrint(xml, { indentSize: 4})
    console.log(prettyStr)
})()

Output of transform()

[
    {
        name: 'Wayne Rooney',
        jerseyNumber: 10,
        yearOfBirth: 1985,
        isRetired: false,
    },
    {
        name: 'Cristiano Ronaldo',
        jerseyNumber: 7,
        yearOfBirth: 1985,
        isRetired: false,
    },
    {
        name: 'Eric Cantona',
        jerseyNumber: 7,
        yearOfBirth: 1966,
        isRetired: true,
    }
]

And output of prettyPrint()

<players>
    <player jerseyNumber="10">
        <name>Wayne Rooney</name>
        <isRetired>false</isRetired>
        <yearOfBirth>1985</yearOfBirth>
    </player>
    <player jerseyNumber="7">
        <name>Cristiano Ronaldo</name>
        <isRetired>false</isRetired>
        <yearOfBirth>1985</yearOfBirth>
    </player>
    <player jerseyNumber="7">
        <name>Eric Cantona</name>
        <isRetired>true</isRetired>
        <yearOfBirth>1966</yearOfBirth>
    </player>
</players>

Licence

The MIT License

Current Tags

  • 6.7.3                                ...           latest (3 months ago)
  • 3.0.20                                ...           legacy-v3 (2 years ago)
  • 6.5.0-rc4                                ...           next (5 months ago)
  • 3.0.19                                ...           oldv3 (7 years ago)

117 Versions

  • 6.7.3                                ...           3 months ago
  • 6.7.2                                ...           4 months ago
  • 6.7.1                                ...           4 months ago
  • 6.7.0                                ...           4 months ago
  • 6.6.1                                ...           4 months ago
  • 6.6.0                                ...           4 months ago
  • 6.5.0                                ...           5 months ago
  • 6.5.0-rc4                                ...           5 months ago
  • 6.5.0-rc3                                ...           5 months ago
  • 6.5.0-rc2                                ...           5 months ago
  • 6.4.0                                ...           5 months ago
  • 6.3.2                                ...           7 months ago
  • 6.3.1                                ...           7 months ago
  • 6.3.0                                ...           7 months ago
  • 6.2.7                                ...           7 months ago
  • 6.2.6                                ...           7 months ago
  • 6.2.5                                ...           7 months ago
  • 6.2.4                                ...           7 months ago
  • 3.0.20                                ...           2 years ago
  • 6.2.3                                ...           3 years ago
  • 6.2.2                                ...           3 years ago
  • 6.2.1                                ...           3 years ago
  • 6.2.1-rc0                                ...           3 years ago
  • 6.2.0                                ...           4 years ago
  • 6.1.0                                ...           6 years ago
  • 6.0.4                                ...           6 years ago
  • 6.0.3                                ...           6 years ago
  • 6.0.2                                ...           6 years ago
  • 6.0.1                                ...           7 years ago
  • 6.0.0 [deprecated]           ...           7 years ago
  • 6.0.0-alpha.1                                ...           7 years ago
  • 6.0.0-alpha.0                                ...           7 years ago
  • 5.0.3                                ...           7 years ago
  • 5.0.2                                ...           7 years ago
  • 5.0.1-alpha.0                                ...           7 years ago
  • 5.0.1                                ...           7 years ago
  • 5.0.0                                ...           7 years ago
  • 5.0.0-alpha.0                                ...           7 years ago
  • 4.2.0                                ...           7 years ago
  • 3.0.19                                ...           7 years ago
  • 4.1.2                                ...           7 years ago
  • 4.1.1                                ...           7 years ago
  • 4.1.0                                ...           7 years ago
  • 4.1.0-alpha.1                                ...           7 years ago
  • 4.1.0-alpha.0                                ...           7 years ago
  • 4.0.8                                ...           7 years ago
  • 4.0.7                                ...           7 years ago
  • 3.0.17                                ...           7 years ago
  • 4.0.5                                ...           7 years ago
  • 4.0.5-alpha.1                                ...           7 years ago
  • 4.0.5-alpha.0                                ...           7 years ago
  • 4.0.4                                ...           7 years ago
  • 4.0.4-alpha.0                                ...           7 years ago
  • 4.0.3                                ...           7 years ago
  • 4.0.2                                ...           8 years ago
  • 4.0.1                                ...           8 years ago
  • 4.0.0                                ...           8 years ago
  • 4.0.0-alpha.8                                ...           8 years ago
  • 4.0.0-alpha.7                                ...           8 years ago
  • 4.0.0-alpha.6                                ...           8 years ago
  • 4.0.0-alpha.5                                ...           8 years ago
  • 4.0.0-alpha.4                                ...           8 years ago
  • 4.0.0-alpha.3                                ...           8 years ago
  • 4.0.0-alpha.2                                ...           8 years ago
  • 3.0.16                                ...           8 years ago
  • 3.0.15                                ...           8 years ago
  • 3.0.14                                ...           8 years ago
  • 3.0.13                                ...           8 years ago
  • 3.0.8                                ...           8 years ago
  • 3.0.7                                ...           8 years ago
  • 3.0.6                                ...           8 years ago
  • 3.0.5                                ...           9 years ago
  • 3.0.4                                ...           9 years ago
  • 3.0.3                                ...           9 years ago
  • 3.0.2                                ...           9 years ago
  • 3.0.1                                ...           9 years ago
  • 3.0.0                                ...           9 years ago
  • 2.2.8                                ...           9 years ago
  • 2.2.7                                ...           9 years ago
  • 2.2.6                                ...           9 years ago
  • 2.2.5                                ...           9 years ago
  • 2.2.4                                ...           9 years ago
  • 2.2.3                                ...           9 years ago
  • 2.2.2                                ...           9 years ago
  • 2.2.1                                ...           9 years ago
  • 2.2.0                                ...           9 years ago
  • 2.1.3                                ...           9 years ago
  • 2.1.2                                ...           9 years ago
  • 2.1.0                                ...           9 years ago
  • 2.0.5                                ...           9 years ago
  • 2.0.4                                ...           9 years ago
  • 2.0.3                                ...           9 years ago
  • 2.0.2                                ...           9 years ago
  • 2.0.1                                ...           9 years ago
  • 2.0.0                                ...           9 years ago
  • 1.5.1                                ...           9 years ago
  • 1.5.0                                ...           9 years ago
  • 1.4.1                                ...           9 years ago
  • 1.3.0                                ...           9 years ago
  • 1.2.0                                ...           9 years ago
  • 1.1.6                                ...           9 years ago
  • 1.1.5                                ...           9 years ago
  • 1.1.4                                ...           9 years ago
  • 1.1.3                                ...           9 years ago
  • 1.1.2                                ...           9 years ago
  • 1.1.1                                ...           9 years ago
  • 1.1.0                                ...           9 years ago
  • 1.0.9                                ...           9 years ago
  • 1.0.8                                ...           9 years ago
  • 1.0.7                                ...           9 years ago
  • 1.0.6                                ...           9 years ago
  • 1.0.5                                ...           9 years ago
  • 1.0.4                                ...           9 years ago
  • 1.0.3                                ...           9 years ago
  • 1.0.2                                ...           9 years ago
  • 1.0.1                                ...           9 years ago
  • 1.0.0                                ...           9 years ago
Maintainers (1)
Downloads
Total 357
Today 0
This Week 0
This Month 161
Last Day 4
Last Week 16
Last Month 81
Dependencies (1)
Dev Dependencies (3)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE