A pure JavaScript W3C standard-based (XML DOM Level 2 Core) DOMParser and XMLSerializer module
npx @tessl/cli install tessl/npm-xmldom--xmldom@0.9.0@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.
npm install @xmldom/xmldomconst { 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 directlyconst { 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);@xmldom/xmldom is structured around several key components:
DOMParser for parsing XML/HTML strings into DOM treesXMLSerializer for converting DOM trees back to XML stringsDocument, Element, Text, etc.DOMException and ParseError classesCore 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>;
}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;
}Convert DOM nodes back into XML strings with optional filtering and formatting control.
class XMLSerializer {
serializeToString(node: Node, nodeFilter?: (node: Node) => boolean): string;
}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;
}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;