JavaScript to XML Parser
js2xmlparser is a Node.js module that converts JavaScript objects into XML. It is currently in its stable version 5.0.0 and has a consistent release cadence with significant updates in major versions. The library specializes in parsing JSON-type objects, arrays, and primitive data types, but also supports native JavaScript objects like `Date` and `RegExp` by leveraging their `toString` methods. A key differentiator is its explicit support for ES2015 `Map` and `Set` objects, enabling ordered XML element generation where standard JSON objects would not guarantee it. It also features robust handling of XML-specific constructs such as attributes (using an `@` property), mixed content (using a `#` property), and multiple elements with the same name (through arrays), alongside pretty-printing capabilities.
Common errors
-
TypeError: js2xmlparser is not a function
cause Attempting to call the module directly as a function after v2.0.0, e.g., `js2xmlparser(root, data, options)`.fixUse the `parse` method: `js2xmlparser.parse(root, data, options)` (CommonJS) or `import { parse } from 'js2xmlparser'; parse(root, data, options);` (ESM). -
ReferenceError: require is not defined
cause Attempting to use `require()` in an ES module context (e.g., in a file where `"type": "module"` is set in `package.json` or a `.mjs` file) without proper transpilation or configuration.fixSwitch to ES module imports: `import { parse } from 'js2xmlparser';` or ensure your environment is configured for CommonJS modules (e.g., `"type": "commonjs"` in `package.json` or use `.cjs` file extension for Node.js). -
The XML output contains unexpected self-closing tags or indentation for multi-line strings.
cause Behavioral changes in XML output formatting introduced in v4.0.0, where self-closing tags are default and multi-line strings are no longer indented by default.fixAdjust options passed to the `parse` method. For example, use `{ selfClosing: false, pretty: { indent: ' ' } }` to override these defaults and regain specific formatting.
Warnings
- breaking The primary API for parsing changed significantly in v2.0.0. Direct invocation of the module (`js2xmlparser(root, data, options)`) was removed.
- breaking Output formatting for multi-line strings and self-closing tags changed in v4.0.0. Multi-line strings are no longer indented by default, and self-closing tags are now the default unless specified otherwise.
- gotcha To define XML attributes and mixed content, specific object properties (`@` for attributes, `#` for mixed content) are used. This requires structuring JavaScript objects specifically for `js2xmlparser`'s conversion logic, which might not be intuitive for standard JSON-to-XML transformations.
- breaking The module was entirely re-written in TypeScript in v2.0.0. While providing types, this could introduce subtle changes in behavior or stricter type checking compared to previous JavaScript versions, potentially affecting existing codebases during migration.
- gotcha As of v5.0.0, type handlers can now be used with bare text and attribute values, but the documentation clarifies they cannot be used with aliases. This might affect custom type handler implementations or assumptions.
Install
-
npm install js2xmlparser -
yarn add js2xmlparser -
pnpm add js2xmlparser
Imports
- parse
const js2xmlparser = require('js2xmlparser'); js2xmlparser(root, data, options);import { parse } from 'js2xmlparser'; - js2xmlparser (CommonJS)
import * as js2xmlparser from 'js2xmlparser';
const js2xmlparser = require('js2xmlparser'); - Options interface
import { Options } from 'js2xmlparser';import type { Options } from 'js2xmlparser';
Quickstart
import { parse } from 'js2xmlparser';
const obj = {
"@": {
type: "natural"
},
firstName: "John",
lastName: "Smith",
dateOfBirth: new Date(1964, 7, 26),
address: {
"@": {
type: "home"
},
streetAddress: "3212 22nd St",
city: "Chicago",
state: "Illinois",
zip: 10000
},
phone: [
{
"@": {
type: "home"
},
"#": "123-555-4567"
},
{
"@": {
type: "cell"
},
"#": "890-555-1234"
},
{
"@": {
type: "work"
},
"#": "567-555-8901"
}
],
email: "john@smith.com"
};
const xmlOutput = parse("person", obj);
console.log(xmlOutput);