Registry / serialization / txml
library5.2.1jsnpmunverified

tXml is a highly optimized and lightweight XML parser designed for high performance across various JavaScript environments, including Node.js, browsers, web workers, and serverless functions. Currently stable at version 5.2.1, the library maintains an active development pace with notable recent updates like the v4 and v5 series. Its core strength lies in its speed, significantly outperforming alternatives like `sax` or `xml2js`, and offering a measurable advantage over `fast-xml-parser` while maintaining a minimal footprint (1.6KB minified + gzipped). tXml distinguishes itself by providing a simple `txml.parse(xml)` API, robust error handling for malformed XML, comprehensive feature support including namespaces, CDATA, and stream processing, and generating an easily traversable DOM-like object. It also offers advanced features such as direct ID/class search on the XML string and simplification options similar to PHP's SimpleXML.

npm install txml
INSTALL
IMPORT
SIG · TXML
T
txml
serializationjavascriptv5.2.1
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

txml.parse
✓ import * as txml from 'txml';
✗ import { parse } from 'txml';
Since v4, the `parse` function is a method of the main `txml` module object. It should be imported as a namespace (`import * as txml from 'txml'`) for ESM/TypeScript or via CommonJS `require` (`const txml = require('txml');`) for `txml.parse()` access.
transformStream
✓ import { transformStream } from 'txml';
✗ import * as txml from 'txml'; txml.transformStream;
The `transformStream` utility is a named export for Node.js stream processing. For smaller browser bundles, consider importing `txml/txml` or `txml/dist/txml` to explicitly exclude Node.js stream components.
XMLNode
✓ import type { XMLNode } from 'txml';
✗ import { XMLNode } from 'txml';
The package provides TypeScript definitions. Common types like `XMLNode` for the parsed DOM structure are available as type imports.

Demonstrates basic synchronous XML parsing using `txml.parse`, including options for simplification and comment preservation, and accessing elements from the resulting DOM object. It also shows filtering for specific nodes.

import * as txml from 'txml'; const xmlString = ` <bookstore> <!-- Best sellers --> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="children"> <title lang="en">Harry Potter</title> <author>J.K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore> `; // Basic parsing, returns an array of nodes const parsedXml = txml.parse(xmlString); console.log('Parsed XML (basic):', JSON.stringify(parsedXml, null, 2)); // Parsing with options: simplify and keep comments const simplifiedXml = txml.parse(xmlString, { simplify: true, keepComments: true }); console.log('Parsed XML (simplified & comments):', JSON.stringify(simplifiedXml, null, 2)); // Accessing elements in the simplified structure const books = simplifiedXml.bookstore.book; if (Array.isArray(books)) { console.log('First book title (simplified):', books[0].title); } else if (books) { console.log('Single book title (simplified):', books.title); } else { console.log('No books found in simplified XML.'); } // Example of using filter to find specific elements const cookingBooks = txml.parse(xmlString, { filter: (node) => node.tagName === 'book' && node.attributes?.category === 'cooking' }); console.log('Cooking books (filtered):', JSON.stringify(cookingBooks, null, 2));
Debug
Known issues
breakingMajor changes in module export structure were introduced in v4. The primary parsing function is now exclusively accessed via `txml.parse()`. Direct named imports for `parse` might no longer work as expected.
fix
Update import statements to `import * as txml from 'txml';` (ESM/TypeScript) or `const txml = require('txml');` (CommonJS), then use `txml.parse(xmlString, options);`.
affects: >=4.0.0
breakingThe `parseStream` method was removed in v4, replaced by `transformStream` for Node.js stream processing.
fix
Migrate stream processing logic to use `transformStream`. Refer to documentation for updated usage patterns, including `for await` loops.
affects: >=4.0.0
gotchaIncluding `transformStream` in your bundle can increase its size, especially for browser applications, as it incorporates Node.js-specific code. It's often not needed client-side.
fix
For smaller browser builds, import the core library via `import * as txml from 'txml/txml';` or `import * as txml from 'txml/dist/txml';` to explicitly exclude the stream components.
affects: >=4.0.0
gotchaBy default, `txml` discards whitespace (spaces, tabs, newlines) between elements. If significant whitespace needs to be preserved (e.g., in mixed content XML or for formatting), the `keepWhitespace` option must be explicitly set to `true`.
fix
Pass `{ keepWhitespace: true }` to the `txml.parse()` options object if whitespace preservation is required.
affects: >=4.0.1
gotchaSince v4.0.1, HTML `hr` tags are treated as self-closing by default. This change might affect parsing behavior for specific HTML-like XML structures where an explicit closing `</hr>` tag was expected or handled differently.
fix
If strict HTML parsing behavior for `hr` and similar tags is required, review and adjust the `noChildNodes` option to customize self-closing element definitions.
affects: >=4.0.1
Errors
Common errors & fixes
TypeError: txml.parse is not a function
Incorrect import pattern for ESM/TypeScript, or trying to destructure `parse` directly from `require('txml')` in CommonJS after v4.0.0.
fix
Ensure `txml` is imported as a namespace (`import * as txml from 'txml';`) or correctly required as a module (`const txml = require('txml');`), then access `parse` as a method (`txml.parse(...)`).
TypeError: Cannot read properties of undefined (reading 'book')
Attempting to access properties on a raw parsed DOM object when `simplify: true` was intended, or incorrect understanding of the simplified object structure.
fix
Ensure `simplify: true` is passed in the options to `txml.parse()` if you expect a flattened, object-like structure for easy property access. The raw parsed output is an array of node objects requiring traversal.
Error: transformStream is not defined / Cannot find name 'transformStream'
The `transformStream` utility was not explicitly imported, or it was excluded from the bundle when using a 'small build' import path in a browser environment.
fix
For Node.js, ensure `transformStream` is explicitly imported: `import { transformStream } from 'txml';`. For browser builds that intend to use `transformStream`, verify you are not using the 'small build' import (`txml/txml` or `txml/dist/txml`) which omits Node.js stream components.
Upgrade
Version history
5.2.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
24 hits · last 30 days
node
21
Amazon
1
OpenAI (training)
1
Resources
txml — npm install txml · libregistry