xmldom-js
Lightweight schema-based XML data extraction to plain objects (JSON)
Example
To read the data from the following XML
MRSS Title Item 1 Item 1 Comment Item 2 Item 2 Comment
Data extraction schema:
; var rssSchema = rss: channel: title: item: title: "media:content": url: "media:comment": ;
First xml is parsed using the browser DOMParser and then read into plain JS objects using the schema:
var rssXml = ; var rssJSON = ;
The resulting JSON:
Namespace handling
A few modes for handling of the namespace are available:
- ignoreNamespace - namespace and prefixes are ignored
- prefixNamespace - use namespace prefixes as is ignoring namespaceURI. Should not be used if XML is provided by third-party as prefixes are not supposed to be stable, but quite safe for in-house XMLs.
- namespaces(defaultURI, { prefix: URI }) - provide map of supported namespace with URIs.
Usage
Npm compatible packager (webpack) is required. CommonJS and ES6 modules are provided, transpiled to es5.