About the XML to JavaScript Converter
There is no built in way to turn XML into a JavaScript object, which is why every project ends up with its own half finished DOM walker. This page skips that step. The object literal mode parses the document and prints the resulting data as JavaScript source with unquoted keys wherever they are valid identifiers, so you can paste a real fixture into a test or a mock server in seconds.
Repeated sibling elements collapse into arrays and nested elements become nested objects, which is the shape most people expect from an XML document. Values are converted rather than left as strings, so a battery percentage arrives as a number and an alarm flag as a boolean, ready for comparisons without a parse step. Empty elements become null.
The class mode reads the structure instead of the data and writes one exported ES class per element shape, each with a constructor that destructures a plain object and assigns the fields, which is a practical base for a small model layer. The JSDoc mode writes @typedef blocks so an editor can autocomplete the shape in a plain JavaScript project with no build step or TypeScript compiler in sight. If your project does use TypeScript, XML to TypeScript produces real interfaces instead.
How to use
- Paste your XML sample or upload the file.
- Pick Object literal with the data to get the parsed content as JavaScript source.
- Pick ES classes or JSDoc typedefs when you want the shape rather than the values.
- Copy the snippet or download it as a
.jsfile.
Common questions
- Does the object literal contain my actual data?
- Yes. That mode converts the document itself, so what you get back is the parsed content ready to use as a fixture.
- Are numbers and booleans converted?
- Yes. Values that look numeric become numbers and the words true and false become booleans, so no extra parsing is needed.
- Why do some keys appear in quotes?
- Keys that are not valid JavaScript identifiers, such as names with a hyphen or a leading digit, are quoted so the literal parses.
- Can the classes parse XML by themselves?
- No. They model the shape. Parse the document with DOMParser first, then pass the plain object into the constructor.