Skip to content
background-image background-image

Data transformation

This example shows how to use

  • fast-xml-parser package for parsing & building XML content
  • js-yaml package for parsing & building YAML content
  • Buffer type for String / Base64 transformation

fast-xml-parser

// 1. Load xml from string

// XMLParser ignores XML attributes by default, now we do not ignore them
const options = {
  ignoreAttributes: false
};

const parser = new XMLParser(options);
const jsonObj = parser.parse('<root a="nice" checked><a>wow</a></root>');

// jsonObj looks like this
{
  root: {
    "@_a": "nice",
    a: "wow",
  },
},


// 2. Parse xml from JSON/object
const builder = new XMLBuilder();
const xml = builder.build({ foo: 'bar'})

// xml string looks like this
// <foo>bar</foo>

js-yaml

// 1. load yaml content
const data = yaml.load("foo: bar\\n");

// data object looks like this
{ foo: 'bar' }

// 2. dump JS object into yaml
const str = yaml.dump({ foo: 'bar' })

// str string looks like this
"foo: bar\\n"

Buffer

// transform string to Base64 back an forth
const base64 = Buffer.from("Hello world").toString('base64');
const data = Buffer.from(base64, 'base64').toString('ascii');

// data looks like this
"Hello world"