Socket
Socket
Sign inDemoInstall

w3c-xmlserializer

Package Overview
Dependencies
1
Maintainers
6
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Protect your apps from supply chain attacks

Install

w3c-xmlserializer

A per-spec XML serializer implementation

    5.0.0latest
    GitHub
    npm

Version published
Maintainers
6
Weekly downloads
20,442,820
increased by3.96%

Weekly downloads

Package description

What is w3c-xmlserializer?

The w3c-xmlserializer npm package is used to serialize DOM nodes to XML strings, following the W3C specification. It is typically used to convert DOM trees into a format that can be easily transported or stored.

What are w3c-xmlserializer's main functionalities?

Serializing DOM Nodes

This feature allows you to serialize a DOM node (such as an entire document or an individual element) to a string that contains XML. The code sample demonstrates how to create an instance of XMLSerializer and use it to serialize a DOM document to an XML string.

const { XMLSerializer } = require('w3c-xmlserializer');
const serializer = new XMLSerializer();
const xmlString = serializer.serializeToString(document);

Other packages similar to w3c-xmlserializer

Readme

Source

w3c-xmlserializer

An XML serializer that follows the W3C specification.

This package can be used in Node.js, as long as you feed it a DOM node, e.g. one produced by jsdom.

Basic usage

Assume you have a DOM tree rooted at a node node. In Node.js, you could create this using jsdom as follows:

const { JSDOM } = require("jsdom");

const { document } = new JSDOM().window;
const node = document.createElement("akomaNtoso");

Then, you use this package as follows:

const serialize = require("w3c-xmlserializer");

console.log(serialize(node));
// => '<akomantoso xmlns="http://www.w3.org/1999/xhtml"></akomantoso>'

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

serialize(node, { requireWellFormed: true });

which will cause Errors to be thrown when non-well-formed constructs are encountered. Per the spec, 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 uses the require-well-formed mode, i.e. trying to get the innerHTML of non-well-formed subtrees will throw.
  • The xhr.send() method does not require well-formedness, i.e. sending non-well-formed Documents will serialize and send them anyway.

Keywords

FAQs

Last updated on 12 Nov 2023

Did you know?

Socket installs a GitHub app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc