Have opinions about JavaScript? We want to hear them. Take the 2018 JavaScript Ecosystem Survey »

xml-string

2.0.1 • Public • Published

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

Keywords

none

install

npm i xml-string

Downloadsweekly downloads

27

version

2.0.1

license

ISC

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability