XML to JavaScript

Generate JavaScript classes or JSDoc typedefs from an XML sample.

XML to JavaScript – Classes and JSDoc for XML data

The XML to JavaScript converter generates plain JavaScript models for data that arrives as XML. Choose ES classes with a static fromJSON factory and a toJSON method, or JSDoc @typedef blocks that give VS Code and TypeScript's checkJs mode full type information without a build step.

How XML maps to types

XML has no native types, so the converter reads every element and attribute value and infers one: whole numbers become integers, decimals become floating-point numbers, true/false become booleans, and everything else stays a string. Attributes are kept apart from child elements (they appear as @name keys internally), an element that has both attributes and text gets a dedicated text field, and repeated sibling elements such as several item nodes become a list. Namespace prefixes are stripped from names so ns:customer and customer produce the same type.

How to use the classes

Parse the XML into a plain object first — for example with fast-xml-parser (attributeNamePrefix: "@"), or in the browser with a small DOMParser walker — then call Order.fromJSON(parsed.order). Nested elements are turned into instances of their own classes, repeated elements into arrays of instances, and toJSON() restores the original keys including @attribute names, so you can serialise the object back to XML with the same library.

Module formats

Export the classes as an ES module (export class), as CommonJS (module.exports), or as a plain script for a <script> tag or the browser console. The JSDoc output pairs well with // @ts-check at the top of a file.

When it helps

Quickly modelling an RSS feed for a static site generator, wrapping a SOAP response in a Node.js service, or documenting the shape of an XML export so teammates know which fields exist.

Tips

Use a sample with every optional element present at least once and two or more repeated items; the converter merges them and marks missing members as optional in the JSDoc.

Format the source with the XML Formatter or generate classes from JSON with JSON to JavaScript.

Frequently Asked Questions

No. The XML is parsed and converted by JavaScript running in your browser tab. Nothing is sent to a server, so internal feeds, SOAP payloads and configuration files stay on your machine.

No. The output is plain modern JavaScript that runs in Node.js and evergreen browsers as-is. JSDoc output is only comments, so it adds no runtime code at all.

Use an XML parser such as fast-xml-parser with ignoreAttributes: false and attributeNamePrefix: "@", or this site's XML to JSON converter for a quick test, then pass the object to fromJSON.

Yes. Set Module to CommonJS to get module.exports, or to Script for a global file without import or export statements.