XML to TypeScript
Automatically generate TypeScript interfaces from your XML structure. Element attributes are prefixed with @, repeated children become arrays, and value types are inferred.
How Interface Generation Works
The generator walks the XML document tree and produces a TypeScript interface
for every distinct element name it encounters. For each element it records which child
element names appear, which attributes exist, and what the element's own text content
looks like - then it emits a typed interface that reflects that structure.
A root Root
interface is always generated for the document element, and every unique child element
name produces its own named interface. Interfaces are sorted so the root appears first,
making the file easy to read top-to-bottom.
Type Inference Rules
| Input value | Inferred type | Example |
|---|---|---|
| Numeric string | number | 42, 3.14 |
| Boolean string | boolean | true, false |
| Any other text | string | "hello", "2024-01-01" |
| Element with child elements | Named interface type | Book |
| Repeated child name | Array type | Item[] |
| XML attribute | Prefixed with @ | "@id": string |
Attribute Naming Convention
XML attributes and child elements share the same namespace within a parent. To avoid
collisions - for example a <book id="1"><id>...</id></book> structure
where both an attribute and a child element are named id - attributes are
prefixed with @ in the generated interface.
This follows the convention used by popular XML-to-JSON libraries such as
fast-xml-parser (with attributeNamePrefix: "@") and
the XML-to-JSON spec proposed by Goessner (2006), making it easy to pair the
generated types with a compliant deserialiser.
Limitations & When to Use a Full Schema Tool
This tool is best for
- Quickly bootstrapping types from a sample document
- Understanding the shape of an unfamiliar XML feed
- Prototyping before writing a full XML Schema (XSD)
- Ad-hoc one-off documents with simple structures
Prefer an XSD-based tool when
- The XML Schema (XSD) is authoritative and available
- You need union types, enumerations, or pattern constraints
- Optional vs. required fields must be precise
- The XML uses namespaces (
xmlns:prefixes)
Because inference is based on a single sample document, a field that can be absent will appear required if it happens to be present in the sample. Similarly, a field that can hold multiple types will only show the type seen in the sample. Always review generated interfaces before using them in production code.
Namespace Handling
XML namespaces (e.g. soap:Envelope, xsi:type) are included
in the local name used for the interface name and field key. The colon is replaced with
an underscore to produce valid TypeScript identifiers
(soap_Envelope). Namespace declarations
(xmlns:* attributes) are omitted from the output as they are
schema meta-data rather than data fields.