2.0.2 • Published 4 years ago

xml-string v2.0.2

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

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>'