Features
With TypesXML you get:
- DOM builder (
DOMBuilder) that preserves lexical information for canonicalization.
- Streaming SAX parser with file, string, and Node.js stream entry points.
- Complete DTD parser and validator supporting parameter entities and conditional sections.
- Default attribute extraction from DTD, Relax NG, or XML Schema grammars.
- OASIS XML Catalog resolver for public and system identifiers.
- Full pass rate on the official W3C XML Conformance Test Suite for DTD grammars.
- Canonical XML renderer aligned with W3C XML Test Suite rules.
- Strict XML 1.0/1.1 character validation with optional DTD-validating mode.
- Pure TypeScript implementation ready for ESM and CommonJS bundlers.
- XML → JSON and JSON → XML converters with simple and metadata-preserving roundtrip modes.
TypesXML stays strictly compatible with open XML standards, keeping your pipelines portable across
tooling ecosystems.
Resources and Assurance
Documentation & Samples
- Step-by-step tutorial for onboarding.
- Hands-on JSON conversion guide covering
roundtrip metadata options.
- Runnable samples covering common workflows.
- Up-to-date README with API notes and release
history.
- NPM distribution with TypeScript definitions included.
Quality & Compatibility
- Automated harness for the W3C XML Conformance Test Suite.
- Canonical XML renderer compatible with W3C expectations.
- Strict XML 1.0/1.1 character validation across parsing modes.
- Deterministic builds from a pure TypeScript codebase—no native dependencies required.
TypeScript/JavaScript Applications
Drop TypesXML into any Node.js or browser pipeline:
import { DOMBuilder, SAXParser } from "typesxml";
const handler = new DOMBuilder();
const parser = new SAXParser();
parser.setContentHandler(handler);
// Parse from a file
parser.parseFile("example.xml");
const document = handler.getDocument();
console.log(document.toString());
// Parse from a string
parser.parseString('<root><child/></root>');
// Parse from a stream
// await parser.parseStream(fs.createReadStream("example.xml"));
JSON and XML Conversion
Translate documents between JSON payloads and XML structures. These examples mirror the tutorial and
show the resulting output so you know what to expect.
JSON to XML
import { XMLDocument, jsonObjectToXmlDocument } from "typesxml";
const data: any = {
library: "painters",
books: ["DaVinci", "VanGogh", "Rubens"],
prices: [13000, 5000, 20000]
};
const document: XMLDocument = jsonObjectToXmlDocument(data, "libraryCatalog");
console.log(document.toString());
Output:
<libraryCatalog>
<library>painters</library>
<books>
<book>DaVinci</book>
<book>VanGogh</book>
<book>Rubens</book>
</books>
<prices>
<price>13000</price>
<price>5000</price>
<price>20000</price>
</prices>
</libraryCatalog>
XML to JSON
import { xmlStringToJsonObject } from "typesxml";
const xml: string = [
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
"<!--Before root-->",
"<libraryCatalog>",
" <library category=\"memo\">painters</library>",
" <books>",
" <book>DaVinci</book>",
" <book>VanGogh</book>",
" <book>Rubens</book>",
" </books>",
"</libraryCatalog>"
].join("\n");
const json: any = xmlStringToJsonObject(xml);
console.log(JSON.stringify(json, null, 2));
Output:
{
"library": {
"_attributes": {
"category": "memo"
},
"_text": "painters"
},
"books": [
"DaVinci",
"VanGogh",
"Rubens"
]
}
Validation & Catalog Configuration
Enable strict validation and wire in XML Catalog resolution before invoking a parse method:
import { Catalog, SAXParser } from "typesxml";
const parser = new SAXParser();
parser.setCatalog(new Catalog("/path/to/catalog.xml"));
parser.setValidating(true); // Turns on DTD validation only.
After configuration, invoke any of the parseFile, parseString, or
parseStream helpers to process XML with validation enabled.