ltx: JavaScript XML Library
ltx is a JavaScript library designed for efficient XML building, parsing, serialization, and manipulation. Currently stable at version 3.1.2, it sees regular maintenance releases primarily focused on dependency updates and minor improvements, with a major version (v3.0.0) introducing significant breaking changes. A key differentiator of ltx is its performance-oriented design, offering its own fast default parser while also supporting integration with multiple third-party parsers like sax-js and saxes for more advanced XML features or specific use cases. It provides a succinct API for in-memory XML object manipulation, supports JSX compatibility via `ltx.createElement`, and offers tagged template literal support for convenient XML string creation. The library targets modern JavaScript environments and Node.js versions 12.4.0 and above. Its modular design allows users to swap out parser backends depending on performance requirements or specific XML feature needs, making it versatile for various XML processing tasks.
Common errors
-
TypeError: element.clone is not a function
cause Attempting to use the `clone` method on an `Element` instance after `ltx` v3.0.0, where this method was removed.fixManually create a new `Element` instance and copy properties/children, or refactor the logic to avoid direct cloning. -
Error: require() of ES Module C:\path\to\node_modules\ltx\lib\index.js from C:\your\app.js not supported.
cause Attempting to `require('ltx')` in a CommonJS module when `ltx` is primarily designed for ESM or has specific dual-package configurations that lead to this conflict in some environments.fixIf working in an ESM context, use `import * as ltx from 'ltx';`. If strictly in CommonJS and encountering this, ensure your `node_modules` are correctly resolved, or consider if an older version of `ltx` is more compatible with your CJS-only setup, although v3.1.0 claims 'Fix dual ESM/CJS export'. -
TypeError: element.toJSON is not a function
cause Calling the `toJSON` method on an `Element` instance after `ltx` v3.0.0, where it was renamed.fixReplace `element.toJSON()` with `element.JSONify()`.
Warnings
- breaking Version 3.0.0 raised the minimum supported Node.js version to 12.4.0. Using older Node.js versions will result in runtime errors due to modern JavaScript syntax.
- breaking As of v3.0.0, `ltx` no longer exports a pre-bundled browser version. If you are targeting browser environments, you must use a module bundler like Webpack, Rollup, or Browserify to package `ltx` for client-side use.
- breaking Several methods on the `Element` class were removed in v3.0.0, including `clone`, `nameEquals`, `attrsEquals`, `childrenEquals`, and `equals`. Attempting to call these methods will result in a `TypeError`.
- breaking The `toJSON` method on `Element` was removed in v3.0.0 and replaced with `JSONify`. Calls to `toJSON` will fail.
- gotcha While `ltx` provides its own fast parser, it is a simpler parser. For advanced XML features (e.g., DTDs, namespaces, validation) or different performance characteristics, you may need to explicitly register and use a third-party parser backend (e.g., `saxes`, `node-expat`).
Install
-
npm install ltx -
yarn add ltx -
pnpm add ltx
Imports
- ltx
import ltx from 'ltx'; // or const ltx = require('ltx');import * as ltx from 'ltx';
- Element
const Element = require('ltx').Element;import { Element } from 'ltx'; - Parser
const Parser = require('ltx').Parser;import { Parser } from 'ltx';
Quickstart
import { Element, Parser } from 'ltx';
// 1. Build an XML element
const book = new Element('book', { id: 'bk101' })
.c('author').t('Gambardella, Matthew').up()
.c('title').t('XML Developer\'s Guide').up()
.c('genre').t('Computer').up();
console.log('Built XML:\n', book.toString());
// 2. Parse an XML string
const xmlString = `<catalog>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<price>4.95</price>
</book>
</catalog>`;
const parser = new Parser();
let parsedCatalog;
parser.on('tree', (tree) => {
parsedCatalog = tree;
});
parser.parse(xmlString);
console.log('\nParsed XML Author:', parsedCatalog.getChild('book').getChild('author').getText());
console.log('Parsed XML Book ID:', parsedCatalog.getChild('book').attrs.id);