Blog.

Parse XML with cheerio and Nodejs

Cover Image for Parse XML with cheerio and Nodejs

Cheerio can parse "almost any HTML / XML document" as they market themselves. Parsing XML is pretty straightforward, all you need to do is set xmlMode: true on cheerio.load method.

Cheerio can parse "almost any HTML / XML document" as they market themselves. Parsing XML is pretty straightforward, all you need to do is set xmlMode: true on cheerio.load method.

Read the documentation for more XML options.

Quick example

const cheerio = require('cheerio')
const fetch = require('isomorphic-unfetch')

const BNR_URL = 'https://www.bnr.ro/nbrfxrates.xml'

fetch(BNR_URL)
  .then((response) => response.text())
  .then((xml) => {
    const $ = cheerio.load(xml, {
      xmlMode: true,
    })
    console.log($('Rate'))
  })

You can try the example in Runkit:

Daniel Turuș

@danielturus

Hi! My name is Daniel and I am a Full-stack JavaScript developer.

If you like my material, please consider following me on Twitter to get notified when new posts are published, ask me a question and stay in touch.