ltx: JavaScript XML Library

3.1.2 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically build an XML element, serialize it to a string, and then parse an XML string to access its properties and children using the `ltx` library's `Element` and `Parser` classes.

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);

view raw JSON →