Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alexdrel/xmldom-js
Lightweight schema-based XML data extraction to plain objects (JSON)
https://github.com/alexdrel/xmldom-js
javascript typescript-library xml xml-parser xml-schema
Last synced: about 2 months ago
JSON representation
Lightweight schema-based XML data extraction to plain objects (JSON)
- Host: GitHub
- URL: https://github.com/alexdrel/xmldom-js
- Owner: alexdrel
- License: mit
- Created: 2018-10-19T17:03:45.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T15:17:48.000Z (about 2 years ago)
- Last Synced: 2024-11-28T19:43:11.320Z (3 months ago)
- Topics: javascript, typescript-library, xml, xml-parser, xml-schema
- Language: TypeScript
- Size: 494 KB
- Stars: 2
- Watchers: 2
- Forks: 2
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/alexdrel/xmldom-js.svg?branch=master)](https://travis-ci.org/alexdrel/xmldom-js)
xmldom-js
===
Lightweight schema-based XML data extraction to plain objects (JSON)### Example
To read the data from the following XML
```xml
MRSS Title
Item 1
Item 1 Comment
Item 2
Item 2 Comment
```Data extraction schema:
```js
import { attribute, content, string, readXML, prefixNamespace } from "xmldom-js";var rssSchema = {
rss: {
channel: {
title: content(string),
item: [{
title: content(string),
"media:content": { url: attribute() },
"media:comment": content(string)
}]
}
}
};
```First xml is parsed using the browser DOMParser and then read into plain JS objects using the schema:
```js
var rssXml = new DOMParser().parseFromString(rssString, "text/xml");var rssJSON = readXML(rssXml, rssSchema, prefixNamespace);
```The resulting JSON:
```json
{
"rss": {
"channel": {
"title": "MRSS Title",
"item": [
{
"title": "Item 1",
"media:content": {
"url": "http://example.com/1.mp4"
},
"media:comment": "Item 1 Comment"
},
{
"title": "Item 2",
"media:content": {
"url": "http://example.com/2.mp4"
},
"media:comment": "Item 2 Comment"
}
]
}
}
}
```### 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.