or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

constants-utilities.mddom-manipulation.mderror-handling.mdindex.mdparsing.mdserialization.md
tile.json

tessl/npm-xmldom--xmldom

A pure JavaScript W3C standard-based (XML DOM Level 2 Core) DOMParser and XMLSerializer module

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@xmldom/xmldom@0.9.x

To install, run

npx @tessl/cli install tessl/npm-xmldom--xmldom@0.9.0

index.mddocs/

@xmldom/xmldom

@xmldom/xmldom is a pure JavaScript ponyfill that provides W3C DOM Level 2 Core APIs for XML parsing and manipulation in Node.js and other ES5-compatible runtimes. It offers comprehensive DOM tree creation, traversal, and serialization capabilities without dependencies.

Package Information

  • Package Name: @xmldom/xmldom
  • Package Type: npm
  • Language: JavaScript (with TypeScript definitions)
  • Installation: npm install @xmldom/xmldom

Core Imports

const { DOMParser, XMLSerializer } = require('@xmldom/xmldom');

For ES modules:

import { DOMParser, XMLSerializer } from '@xmldom/xmldom';

Additional imports (for advanced usage):

const {
  DOMParser,
  XMLSerializer,
  DOMImplementation,
  MIME_TYPE,
  NAMESPACE,
  DOMException,
  ParseError
} = require('@xmldom/xmldom');

Constructor classes are typically accessed through parser results:

const { DOMParser } = require('@xmldom/xmldom');

const parser = new DOMParser();
const doc = parser.parseFromString('<root></root>', 'text/xml');

// Now you have access to DOM instances:
const element = doc.documentElement; // Element instance
const textNode = doc.createTextNode('text'); // Text instance
// Constructor classes (Document, Element, Node, etc.) are available
// but typically not imported directly

Basic Usage

const { DOMParser, XMLSerializer } = require('@xmldom/xmldom');

// Parse XML string into DOM Document
const parser = new DOMParser();
const xmlString = `<root>
  <child id="1">Hello</child>
  <child id="2">World</child>
</root>`;

const doc = parser.parseFromString(xmlString, 'text/xml');

// Access and manipulate DOM
const root = doc.documentElement;
const children = root.getElementsByTagName('child');
console.log(children.length); // 2

// Create new elements
const newChild = doc.createElement('child');
newChild.setAttribute('id', '3');
newChild.textContent = 'New content';
root.appendChild(newChild);

// Serialize back to XML string
const serializer = new XMLSerializer();
const result = serializer.serializeToString(doc);
console.log(result);

Architecture

@xmldom/xmldom is structured around several key components:

  • Core Parsers: DOMParser for parsing XML/HTML strings into DOM trees
  • Serialization: XMLSerializer for converting DOM trees back to XML strings
  • DOM Implementation: Full W3C DOM Level 2 Core node hierarchy with Document, Element, Text, etc.
  • Error Handling: Robust error reporting with DOMException and ParseError classes
  • Standards Compliance: Close adherence to W3C specifications with extensions for parsing configuration
  • Runtime Compatibility: Works across Node.js >=14.6 and ES5-compatible environments

Capabilities

XML/HTML Parsing

Core parsing functionality for converting XML and HTML strings into navigable DOM documents. Supports multiple MIME types and configurable error handling.

class DOMParser {
  constructor(options?: DOMParserOptions);
  parseFromString(source: string, mimeType: string): Document;
}

interface DOMParserOptions {
  locator?: boolean;
  normalizeLineEndings?: (source: string) => string;
  onError?: (level: 'warning' | 'error' | 'fatalError', msg: string, context: any) => void;
  xmlns?: Record<string, string | null | undefined>;
}

XML/HTML Parsing

DOM Tree Manipulation

Complete W3C DOM Level 2 Core implementation for creating, accessing, and modifying document trees. Includes all standard node types and manipulation methods.

class Document extends Node {
  createElement(tagName: string): Element;
  createTextNode(data: string): Text;
  getElementById(elementId: string): Element | null;
  getElementsByTagName(qualifiedName: string): LiveNodeList<Element>;
}

class Element extends Node {
  getAttribute(name: string): string | null;
  setAttribute(name: string, value: string): void;
  getElementsByTagName(name: string): LiveNodeList<Element>;
}

class Node {
  appendChild(child: Node): Node;
  removeChild(child: Node): Node;
  cloneNode(deep?: boolean): Node;
}

DOM Tree Manipulation

XML Serialization

Convert DOM nodes back into XML strings with optional filtering and formatting control.

class XMLSerializer {
  serializeToString(node: Node, nodeFilter?: (node: Node) => boolean): string;
}

XML Serialization

Error Handling

Comprehensive error reporting system with DOM-standard exceptions and parsing-specific error types.

class DOMException extends Error {
  constructor(message?: string, name?: string);
  readonly name: string;
  readonly code: number;
}

class ParseError extends Error {
  constructor(message: string, locator?: any, cause?: Error);
  readonly locator?: any;
}

Error Handling

Constants and Utilities

Standard DOM constants, MIME type definitions, namespace URIs, and utility functions for validation and type checking.

const MIME_TYPE = {
  HTML: 'text/html',
  XML_APPLICATION: 'application/xml',
  XML_TEXT: 'text/xml',
  XML_XHTML_APPLICATION: 'application/xhtml+xml',
  XML_SVG_IMAGE: 'image/svg+xml'
};

const NAMESPACE = {
  HTML: 'http://www.w3.org/1999/xhtml',
  SVG: 'http://www.w3.org/2000/svg',
  XML: 'http://www.w3.org/XML/1998/namespace',
  XMLNS: 'http://www.w3.org/2000/xmlns/'
};

function isValidMimeType(mimeType: string): boolean;
function normalizeLineEndings(input: string): string;

Constants and Utilities