or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

dom-manipulation.mderror-handling.mdindex.mdnamespace-support.mdxml-parsing.mdxml-serialization.md
tile.json

tessl/npm-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@0.6.x

To install, run

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

index.mddocs/

xmldom

xmldom is a pure JavaScript implementation of the W3C DOM (Document Object Model) Level 2 Core specification, providing complete XML parsing and manipulation capabilities for Node.js, Rhino, and browser environments. It offers standard DOMParser and XMLSerializer interfaces that are fully compatible with browser implementations, enabling seamless XML document processing across different JavaScript environments.

Package Information

  • Package Name: xmldom
  • Package Type: npm
  • Language: JavaScript (ES5 compatible)
  • Installation: npm install xmldom

Core Imports

const { DOMParser, XMLSerializer, DOMImplementation, Node, DOMException } = require('xmldom');

For ES6/TypeScript:

import { DOMParser, XMLSerializer, DOMImplementation, Node, DOMException } from 'xmldom';

Basic Usage

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

// Parse XML string into DOM document
const parser = new DOMParser();
const doc = parser.parseFromString(`
  <books>
    <book id="1" title="XML Processing" />
    <book id="2" title="DOM Manipulation" />
  </books>
`, 'text/xml');

// Access and manipulate DOM
const books = doc.getElementsByTagName('book');
console.log(books.length); // 2
console.log(books.item(0).getAttribute('title')); // "XML Processing"

// Add new element
const newBook = doc.createElement('book');
newBook.setAttribute('id', '3');
newBook.setAttribute('title', 'JavaScript XML');
doc.documentElement.appendChild(newBook);

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

Architecture

xmldom implements the complete W3C DOM Level 2 Core specification with the following key components:

  • Parser Interface: DOMParser for converting XML strings to DOM documents with robust error handling
  • Serializer Interface: XMLSerializer for converting DOM nodes back to XML strings
  • DOM Node Hierarchy: Complete implementation of Node, Document, Element, Attr, Text, and other DOM node types
  • Collection Interfaces: NodeList and NamedNodeMap for managing collections of nodes and attributes
  • Namespace Support: Full XML Namespace support with namespace-aware methods and properties
  • Error Handling: Comprehensive DOMException system with detailed error codes and messages
  • SAX-style Parsing: Internal SAX parser for efficient XML processing with configurable error handlers

Capabilities

XML Parsing

Core XML parsing functionality with configurable error handling, namespace support, and both XML and HTML parsing modes.

class DOMParser {
  constructor(options?: {
    locator?: object;
    errorHandler?: {
      warning?: (msg: string) => void;
      error?: (msg: string) => void;
      fatalError?: (msg: string) => void;
    } | ((level: string, msg: string) => void);
    xmlns?: object;
    domBuilder?: object;
  });
  
  parseFromString(source: string, mimeType: string): Document;
}

XML Parsing

DOM Manipulation

Complete DOM manipulation capabilities including document creation, element manipulation, attribute handling, and tree traversal.

interface Document extends Node {
  readonly doctype: DocumentType | null;
  readonly implementation: DOMImplementation;
  readonly documentElement: Element | null;
  
  createElement(tagName: string): Element;
  createTextNode(data: string): Text;
  createComment(data: string): Comment;
  getElementById(elementId: string): Element | null;
  getElementsByTagName(tagname: string): NodeList;
  getElementsByClassName(className: string): NodeList;
}

interface Element extends Node {
  readonly tagName: string;
  
  getAttribute(name: string): string;
  setAttribute(name: string, value: string): void;
  removeAttribute(name: string): void;
  getElementsByTagName(name: string): NodeList;
  hasAttribute(name: string): boolean;
}

DOM Manipulation

XML Serialization

XML serialization functionality for converting DOM nodes and documents back to XML strings with proper formatting and namespace handling.

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

XML Serialization

Namespace Support

Comprehensive XML Namespace support with namespace-aware methods, prefix handling, and namespace URI resolution.

interface Element extends Node {
  getAttributeNS(namespaceURI: string, localName: string): string;
  setAttributeNS(namespaceURI: string, qualifiedName: string, value: string): void;
  removeAttributeNS(namespaceURI: string, localName: string): void;
  getElementsByTagNameNS(namespaceURI: string, localName: string): NodeList;
  hasAttributeNS(namespaceURI: string, localName: string): boolean;
}

interface Document extends Node {
  createElementNS(namespaceURI: string, qualifiedName: string): Element;
  createAttributeNS(namespaceURI: string, qualifiedName: string): Attr;
  getElementsByTagNameNS(namespaceURI: string, localName: string): NodeList;
}

Namespace Support

Error Handling

Robust error handling system with DOMException classes, configurable error handlers, and detailed parsing error reporting.

class DOMException extends Error {
  readonly code: number;
  static readonly INDEX_SIZE_ERR: 1;
  static readonly DOMSTRING_SIZE_ERR: 2;
  static readonly HIERARCHY_REQUEST_ERR: 3;
  static readonly WRONG_DOCUMENT_ERR: 4;
  static readonly INVALID_CHARACTER_ERR: 5;
  // ... additional error codes
}

Error Handling

Types

interface Node {
  readonly nodeName: string;
  nodeValue: string | null;
  readonly nodeType: number;
  readonly parentNode: Node | null;
  readonly childNodes: NodeList;
  readonly firstChild: Node | null;
  readonly lastChild: Node | null;
  readonly previousSibling: Node | null;
  readonly nextSibling: Node | null;
  readonly attributes: NamedNodeMap | null;
  readonly ownerDocument: Document | null;
  readonly namespaceURI: string | null;
  prefix: string | null;
  readonly localName: string | null;
  textContent: string | null;
  
  insertBefore(newChild: Node, refChild: Node | null): Node;
  replaceChild(newChild: Node, oldChild: Node): Node;
  removeChild(oldChild: Node): Node;
  appendChild(newChild: Node): Node;
  hasChildNodes(): boolean;
  cloneNode(deep: boolean): Node;
  normalize(): void;
  isSupported(feature: string, version: string): boolean;
  hasAttributes(): boolean;
}

interface NodeList {
  readonly length: number;
  item(index: number): Node | null;
}

interface NamedNodeMap {
  readonly length: number;
  getNamedItem(name: string): Node | null;
  setNamedItem(arg: Node): Node | null;
  removeNamedItem(name: string): Node;
  item(index: number): Node | null;
  getNamedItemNS(namespaceURI: string, localName: string): Node | null;
  setNamedItemNS(arg: Node): Node | null;
  removeNamedItemNS(namespaceURI: string, localName: string): Node;
}

interface DOMImplementation {
  hasFeature(feature: string, version: string): boolean;
  createDocumentType(qualifiedName: string, publicId: string, systemId: string): DocumentType;
  createDocument(namespaceURI: string | null, qualifiedName: string | null, doctype: DocumentType | null): Document;
}

// DOM Node Type Constants
const ELEMENT_NODE: 1;
const ATTRIBUTE_NODE: 2;
const TEXT_NODE: 3;
const CDATA_SECTION_NODE: 4;
const ENTITY_REFERENCE_NODE: 5;
const ENTITY_NODE: 6;
const PROCESSING_INSTRUCTION_NODE: 7;
const COMMENT_NODE: 8;
const DOCUMENT_NODE: 9;
const DOCUMENT_TYPE_NODE: 10;
const DOCUMENT_FRAGMENT_NODE: 11;
const NOTATION_NODE: 12;