xml-string

Utility to create XML strings. Can be used to write any XML based files such as SVG, HTML or GPX.

Usage no npm install needed!

<script type="module">
  import xmlString from 'https://cdn.skypack.dev/xml-string';
</script>

README

xml-string

Utility to create XML strings. Can be used to write any XML based files such as SVG, HTML or GPX.

Install

$ npm install xml-string

Usage

const xml = require('xml-string')

.create()

const svg = xml.create('svg')

.child()

Append a child element

const svg = xml.create('svg')
const g = svg.child('g')

.attr()

Add attributes

const svg = xml.create('svg')
const g = svg.child('g')
g.attr({ id: 'group' })

.data()

Add data

const svg = xml.create('svg')
const g = svg.child('g')
g.attr({ id: 'group' })
const text = g.child('text')
text.data('Hello world')

.outer()

Returns the XML string

const svg = xmlString.create('svg')
const g = svg.child('g')
g.attr({ id: 'group' })
const text = g.child('text')
text.data('Hello world')

console.log(svg.outer())
// returns
// '<svg><g id="group"><text>Hello world</text></g></svg>'

Aliases

  • .child() > .c()
  • .attr() > .a()
  • .data() > .d()

Example:

const svg = xml.create('svg')
const g = svg.c('g')
g.a({ id:'group' })
const text = g.c('text')
text.d('Hello world')

console.log(svg.outer())
// returns
// '<svg><g id="group"><text>Hello world</text></g></svg>'