simple-xml-dom

A thin wrapper around DOMParser and XMLSerializer for node and the browser.

Usage no npm install needed!

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

README

simple-xml-dom

simple-xml-dom is a thin wrapper around DOMParser and XMLSerializer that makes parsing and serializing XML more pleasant. It depends on xmldom in node, and native implementations in the browser.

Build status

Install

$ npm install simple-xml-dom

note: xmldom is not installed alongside simple-xml-dom

Usage

Basic

The available methods are parse and serialize.

var xml = require('simple-xml-dom')

// Parse 
var dom = xml.parse('<hello>world</hello>')
dom.documentElement.textContent
> 'world'

// Serialize
dom.documentElement.textContent = 'dexter morgan'
xml.serialize(dom)
> '<hello>dexter morgan</hello>'

A more modular approach

Sometimes you only need to parse or serialize, and in these cases it makes sense to only require a single method.

var parse = require('simple-xml-dom/parse')
var serialize = require('simple-xml-dom/serialize')

serialize(parse('<hello>world<hello>'))
> '<hello>world</hello>'

Format correction

XMLSerialize collapses the whitespace surrounding declarations, so the format method exists to restore these characters.

var xml = require('simple-xml-dom')
var format = require('simple-xml-dom/format')

var source = '<?xml version="1.0" encoding="UTF-8"?>\r\n<hello>world</hello>'

// Whitespace collapsed
xml.serialize(xml.parse(source))
> '<?xml version="1.0" encoding="UTF-8"?><hello>world</hello>'

// Whitespace restored
format(xml.serialize(xml.parse(source)), source)
> '<?xml version="1.0" encoding="UTF-8"?>\r\n<hello>world</hello>'

The second argument is optional. If present, whitespace from source will be adopted by the serialize output’s declarations. If not, a new-line character will be inserted after every declaration.

License

MIT