xmldoc is a lightweight JavaScript library designed for parsing and traversing XML documents. It provides a simple, object-oriented API for interacting with XML structures, backed by the robust `sax` parser. The current stable version is 2.0.3, with releases occurring as needed for bug fixes and feature enhancements, rather than on a strict cadence. A key differentiator is its minimal footprint and direct access to XML elements and attributes, avoiding more complex XPath implementations by default. Since version 2.0, it fully supports TypeScript, offering type definitions for a better developer experience, and is compatible with both CommonJS and ESM environments. It focuses on parsing and basic traversal, making it suitable for applications where full XML DOM manipulation or complex querying (like advanced XPath) is not required.
npm install xmldocVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates parsing an XML string, traversing elements, accessing attributes and text content, and converting the document back to a string.
Update code to check for `undefined` instead of `null` or use loose equality `== null` to catch both `null` and `undefined`.
For CommonJS, use `const { XmlDocument } = require('xmldoc');`. For ESM and TypeScript, use `import { XmlDocument } from 'xmldoc';`.If comment preservation is critical, `xmldoc` is not suitable for round-trip XML modification that must retain comments. Consider alternative XML libraries or pre-process/post-process comments if possible.
Do not rely strictly on the exact original order of sibling elements if your application is sensitive to it. Test thoroughly and consider alternative parsers if strict order preservation is paramount for all operations.
For CommonJS, use `const { XmlDocument } = require('xmldoc');`. For ESM, use `import { XmlDocument } from 'xmldoc';`.Upgrade `xmldoc` to version `0.5.1` or higher to resolve the `GLOBAL` deprecation warning/error in newer Node.js environments. Alternatively, ensure your Node.js version is compatible with the `xmldoc` version in use.
Comments are not preserved by design. If you are experiencing incorrect character escaping, upgrade `xmldoc` to `v0.5.0` or newer, which fixed the incorrect escaping issue.