--- title: How to use Camaro to parse XML to JSON excerpt: A brief guide on XPath date: 2020-01-20 tags: - javascript --- [Camaro](https://github.com/tuananh/camaro) is a NodeJS library to transform XML to JSON using [XPath](https://developer.mozilla.org/en-US/docs/Web/XPath) template format. ### Usage ``` js const { transform } = require('camaro') const parseFeed = async (input) => { // Specify input in the 1st parameter, template in the 2nd parameter const output = await transform(input, { text: 'feed' }) console.log(output) } parseFeed(` foo `) // { text: 'foo' } ``` The following examples use Atom/RSS2 format for illustration purpose only and do not necessarily conform to the W3C standard. ### Text Text value of an element - Input: ``` xml foo ``` - Template: `{ text: 'feed' }` - Output: `{ text: 'foo' }` ### Text (second level) Text value of an element located in second level - Input: ``` xml foo ``` - Template: `{ text: 'feed/title' }` - Output: `{ text: 'foo' }` ### Text (all level) If the element is located in very deep level, instead of using `locate/very/deep/level`, you can use double-slash prefix. This only works if the element name is unique. - Input: ``` xml foo ``` - Template: `{ text: '//title' }` - Output: `{ text: 'foo' }` ### Text (distinct elements) - Input: ``` xml foo 2020 ``` - Template: `{ title: 'feed/title', updated: 'feed/updated' }` - Output: `{ title: 'foo', updated: '2020' }` ### Attribute Parse value of an attribute - Input: ``` xml ``` - Template: `{ feedLnk: 'feed/link/@href' }` - Output: `{ feedLnk: 'example.com' }` ### Text (multiple elements) Parse all entries of a specified element - Input: ``` xml first second ``` - Template: ``` js { items: [ 'feed/entry', { title: 'title' } ] } ``` - Output: ``` json { "items": [ { "title": "first" }, { "title": "second" } ] } ``` ### Text (similar elements, similar sub-entries) Parse all sub-entries of a specified element - Input: ``` xml title A apple orange title B peach mango ``` - Template: ``` js { items: [ 'feed/entry', { title: 'title', category: [ 'category', '.' ] } ] } ``` - Output: ``` json { "items": [ { "title": "title A", "category": [ "apple", "orange" ] }, { "title": "title B", "category": [ "peach", "mango" ] } ] } ``` ### Attribute (multiple elements, multiple sub-entries) Parse all sub-entries of a specified element - Input: ``` xml title A title B ``` - Template: ``` js { items: [ 'feed/entry', { title: 'title', category: [ 'category', '@term' ] } ] } ``` - Output: ``` json { "items": [ { "title": "title A", "category": [ "apple", "orange" ] }, { "title": "title B", "category": [ "peach", "mango" ] } ] } ```