simple-xml-dom
A thin wrapper around DOMParser and XMLSerializer for node and the browser.
Last updated 6 years ago by michaelrhodes .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install simple-xml-dom 
SYNC missed versions from official npm registry.

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

Current Tags

  • 1.0.0                                ...           latest (6 years ago)

1 Versions

  • 1.0.0                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 4
Last Day 0
Last Week 4
Last Month 6
Dependencies (0)
None
Dev Dependencies (3)

Copyright 2014 - 2017 © taobao.org |