https://github.com/jsdom/w3c-xmlserializer
An XML serializer that follows the W3C specification.
https://github.com/jsdom/w3c-xmlserializer
Last synced: about 1 year ago
JSON representation
An XML serializer that follows the W3C specification.
- Host: GitHub
- URL: https://github.com/jsdom/w3c-xmlserializer
- Owner: jsdom
- License: mit
- Created: 2018-06-03T07:50:16.000Z (about 8 years ago)
- Default Branch: main
- Last Pushed: 2023-11-12T07:59:11.000Z (over 2 years ago)
- Last Synced: 2025-03-31T08:08:51.920Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 275 KB
- Stars: 21
- Watchers: 4
- Forks: 11
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# w3c-xmlserializer
An XML serializer that follows the [W3C specification](https://w3c.github.io/DOM-Parsing/).
This package can be used in Node.js, as long as you feed it a DOM node, e.g. one produced by [jsdom](https://github.com/jsdom/jsdom).
## Basic usage
Assume you have a DOM tree rooted at a node `node`. In Node.js, you could create this using [jsdom](https://github.com/jsdom/jsdom) as follows:
```js
const { JSDOM } = require("jsdom");
const { document } = new JSDOM().window;
const node = document.createElement("akomaNtoso");
```
Then, you use this package as follows:
```js
const serialize = require("w3c-xmlserializer");
console.log(serialize(node));
// => ''
```
## `requireWellFormed` option
By default the input DOM tree is not required to be "well-formed"; any given input will serialize to some output string. You can instead require well-formedness via
```js
serialize(node, { requireWellFormed: true });
```
which will cause `Error`s to be thrown when non-well-formed constructs are encountered. [Per the spec](https://w3c.github.io/DOM-Parsing/#dfn-require-well-formed), this largely is about imposing constraints on the names of elements, attributes, etc.
As a point of reference, on the web platform:
* The [`innerHTML` getter](https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml) uses the require-well-formed mode, i.e. trying to get the `innerHTML` of non-well-formed subtrees will throw.
* The [`xhr.send()` method](https://xhr.spec.whatwg.org/#the-send()-method) does not require well-formedness, i.e. sending non-well-formed `Document`s will serialize and send them anyway.