A pure Node.js library for solving diverse XML-related application tasks with minimal resources and dependencies.
node-xml-toolkit handles XML parsing, marshalling, and SOAP integration — from streaming multi-gigabyte files to invoking SOAP 1.1 web services via WSDL.
Features
- Dual parsing modes: Fast synchronous parser for small documents; streaming asynchronous parser for large files
- Memory-efficient: Scan multi-GB XML files with limited buffer
- Schema support: Validate and serialize objects using XML Schema (XSD)
- SOAP 1.1, 1.2 adapters: Invoke web services directly from WSDL + plain JS objects
- Flexible output: Format, patch, or transform XML with configurable options
- Stream-ready: Works with Node.js streams, async iterators, and event emitters
Installation
npm install xml-toolkit
Quick Start
Parse a Small XML File (Sync)
const fs = require('fs');
const { XMLParser, XMLSchemata } = require('xml-toolkit');
const xml = fs.readFileSync('doc.xml');
const parser = new XMLParser(); // optionally: { xs: new XMLSchemata('schema.xsd') }
const document = parser.process(xml);
for (const element of document.detach().children) {
console.log(element.attributes);
}
Stream Large XML Files (Async)
const { XMLReader, XMLNode } = require('xml-toolkit');
const records = new XMLReader({
filterElements: 'Record',
map: XMLNode.toObject({})
}).process(xmlSource);
// Use as async iterator
for await (const record of records) {
// process each record
}
// Or pipe to another stream
// records.pipe(nextStream);
Extract a Single Element
const { XMLReader, XMLNode } = require('xml-toolkit');
const data = await new XMLReader({
filterElements: 'MyElementName',
map: XMLNode.toObject({})
}).process(xmlSource).findFirst();
Format / Pretty-Print XML
const { XMLParser } = require('xml-toolkit');
const formatted = new XMLParser()
.process(fs.readFileSync('doc.xml'))
.toString({
space: '\t', // indentation
// EOL: '\n', // line ending
// encodeLineBreaks: false
});
Serialize Objects to XML (via XSD)
const { XMLSchemata } = require('xml-toolkit');
const data = { ExportDebtRequestsResponse: { 'request-data': { /* ... */ } } };
const xs = new XMLSchemata('schema.xsd');
const xml = xs.stringify(data);
// → <ns0:ExportDebtRequestsResponse xmlns:ns0="urn:...">...
Invoke a SOAP 1.1 Web Service
const http = require('http');
const { SOAP11 } = require('xml-toolkit');
const soap = new SOAP11('service.wsdl');
const { method, headers, body } = soap.http({
RequestElementNameOfTheirs: { amount: '0.01' }
});
const req = http.request(endpointURL, { method, headers });
req.write(body);
req.end();
Core API Overview
| Class / Export | Purpose | Mode |
|---|---|---|
XMLParser |
Synchronous full-document parsing | Sync |
XMLReader |
Streaming parser with filtering & mapping | Async/Stream |
XMLNode |
DOM-like node representation + utilities | Both |
XMLSchemata |
XSD-based validation & object→XML serialization | Both |
SOAP |
SOAP adapter using WSDL | Sync setup |
Limitations
- No DTD support: Entity declarations may cause parser errors
- XML Schema is "forever beta": Features like
xs:uniqueare not implemented; validation coverage is partial - For production-grade validation, consider external tools like
xmllint(used in the library's test suite)
Motivation
Node.js lacks built-in XML tooling comparable to Java's JAXB/JAX-WS. While many pure-JS XML modules exist, node-xml-toolkit was created to deliver most necessary functions with:
- Minimal computing resources
- Minimal application code
- Almost no external dependencies
All in a pure JavaScript implementation.
License
MIT do-