XML to JavaScript Class Generator
CV-82Paste an XML document and press Convert to get ES classes or constructor functions. 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
JavaScript
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 an ES class or a constructor function, and keep or drop the JSDoc comments. 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 .js file.
- Build the model with new Root(JSON.parse(jsonText)). Feed it the JSON form of your XML, not the XML itself.
How the classes are built
Each object becomes an ES class, or a constructor function if you choose one, whose constructor copies the keys from a parsed JSON object. Keys that are valid identifiers keep their name; others become camelCase and are read with data["key"]. JSDoc comments give each type, with |null for null keys and a note on keys missing from some objects. Nothing is exported, and no getters, setters or validation are written.
How the XML becomes classes
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. The root element becomes the single top-level key, so the first type holds one member named after it. In JavaScript, @id becomes the property id and is read with data["@id"].
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 strings, and an integer beyond 9007199254740991 is already rounded by JSON.parse before the class sees it. 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 JavaScript class?
Paste the XML into the input box, choose your options and press Convert to get the classes. Build the model with new Root(JSON.parse(jsonText)). The classes follow the JSON form of the XML. The conversion happens in your browser.
How does the JavaScript output handle nested objects and arrays?
A nested object is built with new on its own class, and an array of objects is mapped into instances, so the result is a tree of class instances rather than plain data. Arrays of strings and numbers are kept as they are. This applies to the JSON form of your XML.
Does the JavaScript converter handle keys that are not valid names?
Yes. A key such as first-name is read with data["first-name"] and stored under the camelCase property firstName. Keys that are already valid identifiers keep their name. A key named constructor or __proto__ gets a trailing underscore. This applies to the JSON form of your XML.
Why are there JSDoc comments, and can I remove them?
They give editors a type for each property, for example number, or string|null for null keys, plus a note on keys that are missing from some objects. Turn the JSDoc option off to get bare classes with no comments. This applies to the JSON form of your XML.
What happens to XML attributes in the generated code?
Each attribute becomes a key that starts with @, such as @id, so it is a member of the generated type next to the child elements. The member name drops the @, and the original key is kept in the code where the language allows it. The JavaScript classes are written from that converted data.
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 JavaScript classes 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 ES class (or a constructor function) whose constructor copies the keys from a parsed object, and nested objects and lists of objects are turned into instances of their own classes. Keys that are valid identifiers are used as property names; other keys are renamed to camelCase and read with data["key"]. JSDoc comments give each type, and an integer beyond 9007199254740991 is typed number even though JSON.parse rounds it. Missing keys stay undefined. There are no getters, setters or validation, and the output has no export line, so add one to match your module system. Everything runs in your browser; nothing is uploaded.