XML to TypeScript Interface Generator
CV-86Paste an XML document and press Convert to get interfaces with optional and union types. The XML is read into JSON first, so attributes, text and repeated elements follow the rules below, and it runs in your browser.
Runs in your browser. Nothing is uploaded.
XML
TypeScript
How to use it
- Paste an XML document, or open an .xml file. Use one that shows every element you expect, with repeating elements repeated.
- Choose interface or type, optional keys, the export keyword and the indent. Leave typed values on to read 59.90 as a number and true as a boolean, or turn it off to keep every value as text.
- Press Convert, then copy the result or download it as a .ts file.
- Use the interface as a type for the parsed data, for example JSON.parse(text) as Root; nothing is checked at runtime. Feed it the JSON form of your XML, not the XML itself.
How the interfaces are built
Each object becomes an interface, or a type alias if you choose it, and nested objects get their own named interfaces. Every number is typed number, with a comment on integers beyond 9007199254740991, because JSON.parse rounds those. Mixed types become a union, and an empty array becomes unknown[]. Nested interface names come from the key, and arrays use the key without its trailing s; the export keyword and indent can be changed.
How the XML becomes interfaces
An element that repeats under the same parent becomes a list, while an element that appears once stays a single value. Text that is exactly a number or true/false is typed as such when the option is on (the default); a leading zero or plus sign keeps it as a string. Comments, processing instructions and the DOCTYPE are dropped, and namespace prefixes stay in the name. The XML is read into JSON first, using the same rules as the XML to JSON converter, and the code is built from that JSON. In TypeScript the key stays as written, so the interface has "@id" and "#text" members.
What a sample cannot tell it
The result only reflects the sample you paste: a key that never appears has no member, and a number that is always whole in the sample is typed as whole even if other data holds decimals. Dates stay string, and no runtime check is written, so validate data that comes from outside. An element that appears once in your sample is written as a single value; if real documents can repeat it, paste a sample where it appears at least twice so it becomes a list.
Questions people ask
How do I convert XML to a TypeScript interface?
Paste the XML into the input box, choose your options and press Convert to get the interfaces. Use the interface as a type for the parsed data, for example JSON.parse(text) as Root; nothing is checked at runtime. The interfaces follow the JSON form of the XML. The conversion happens in your browser.
Why is a field optional (?) in the TypeScript interface?
A key missing from some objects in the sample becomes optional with ?. A key that is null in some items becomes | null instead. The optional-keys checkbox lets you turn the ? off if you want every key required. This applies to the JSON form of your XML.
Does the TypeScript output check data at runtime?
No. Interfaces exist only at compile time, so JSON.parse(text) as Root is a promise to the compiler, not a check. Validate data that comes from outside with a schema validator; the JSON Schema generators on this site can start one for you. This applies to the JSON form of your XML.
Why is a big integer typed number with a comment in TypeScript?
TypeScript has one number type, and JSON.parse rounds integers beyond 9007199254740991. The line gets a comment so you know the value may already be rounded. Keep such values as strings in the source data. This applies to the JSON form of your XML.
Why is an element a list in one XML sample and a single value in another?
XML does not say how many times an element may repeat. An element becomes a list only when it appears more than once under the same parent in your sample. To get a list type, paste a sample where the element appears at least twice. The TypeScript interfaces are written from that converted data.
Does it read the XML declaration, comments or namespaces?
The declaration, comments, processing instructions and DOCTYPE are read and then dropped. Namespace prefixes stay as part of the element name, and namespaces are not resolved. Custom entities are an error; only the five built-in ones and numeric character references are decoded. The TypeScript interfaces are written from that converted data.
About this tool
The XML is read first and turned into the same JSON that the XML to JSON converter writes, and the code is generated from that. The root element becomes the single top-level key, so the root class holds one field named after it. An attribute becomes a key that starts with @, text next to attributes or child elements becomes #text, an element that appears more than once under one parent becomes a list, and one that appears once stays a single value, so a sample with one item gives a single object where another sample would give a list. Comments, processing instructions and the DOCTYPE are dropped. With the typed-values option on (the default), text that is exactly a number or true/false becomes a number or boolean; text with a leading zero or plus sign stays a string, and turning the option off makes every value a string. The result describes the converted JSON, not an XmlSerializer or JAXB mapping. Every object becomes an interface (or a type alias), and nested objects get their own interfaces named after the key. A key that is missing from some objects becomes optional (?), a null becomes | null, mixed types become a union, and every number is typed number, with a comment on integers beyond 9007199254740991 because JSON.parse rounds them. An empty array becomes unknown[]. It does not add runtime checks. Everything runs in your browser; nothing is uploaded.