{"id":11161,"library":"js2xmlparser","title":"JavaScript to XML Parser","description":"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.","status":"active","version":"5.0.0","language":"javascript","source_language":"en","source_url":"git://github.com/michaelkourlas/node-js2xmlparser","tags":["javascript","convert","converter","js","json","object","objects","parse","typescript"],"install":[{"cmd":"npm install js2xmlparser","lang":"bash","label":"npm"},{"cmd":"yarn add js2xmlparser","lang":"bash","label":"yarn"},{"cmd":"pnpm add js2xmlparser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Since v2.0.0, the primary parsing function is a named export `parse` when using ES modules or accessed via `js2xmlparser.parse` from a CommonJS import. Direct invocation of the module `js2xmlparser(...)` was removed in v2.0.0.","wrong":"const js2xmlparser = require('js2xmlparser'); js2xmlparser(root, data, options);","symbol":"parse","correct":"import { parse } from 'js2xmlparser';"},{"note":"For CommonJS environments, `require` is used. The `parse` method is then called as `js2xmlparser.parse(root, data, options)`. While `import * as` is valid ESM, the CJS pattern is explicit for older Node.js projects.","wrong":"import * as js2xmlparser from 'js2xmlparser';","symbol":"js2xmlparser (CommonJS)","correct":"const js2xmlparser = require('js2xmlparser');"},{"note":"Type definitions, including the `Options` interface, are exported by the package. Use `import type` for type-only imports to prevent potential bundling or runtime issues, especially since v4.0.2 exports these interfaces.","wrong":"import { Options } from 'js2xmlparser';","symbol":"Options interface","correct":"import type { Options } from 'js2xmlparser';"}],"quickstart":{"code":"import { parse } from 'js2xmlparser';\n\nconst obj = {\n    \"@\": {\n        type: \"natural\"\n    },\n    firstName: \"John\",\n    lastName: \"Smith\",\n    dateOfBirth: new Date(1964, 7, 26),\n    address: {\n        \"@\": {\n            type: \"home\"\n        },\n        streetAddress: \"3212 22nd St\",\n        city: \"Chicago\",\n        state: \"Illinois\",\n        zip: 10000\n    },\n    phone: [\n        {\n            \"@\": {\n                type: \"home\"\n            },\n            \"#\": \"123-555-4567\"\n        },\n        {\n            \"@\": {\n                type: \"cell\"\n            },\n            \"#\": \"890-555-1234\"\n        },\n        {\n            \"@\": {\n                type: \"work\"\n            },\n            \"#\": \"567-555-8901\"\n        }\n    ],\n    email: \"john@smith.com\"\n};\n\nconst xmlOutput = parse(\"person\", obj);\nconsole.log(xmlOutput);","lang":"typescript","description":"Demonstrates parsing a complex JavaScript object into XML, showcasing attribute handling, nested objects, array elements, and mixed content using the `parse` function."},"warnings":[{"fix":"Migrate all calls to `js2xmlparser.parse(root, data, options)` or `parse(root, data, options)` if using named imports.","message":"The primary API for parsing changed significantly in v2.0.0. Direct invocation of the module (`js2xmlparser(root, data, options)`) was removed.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Review existing XML output expectations and adjust options as needed (e.g., `selfClosing: false` to disable self-closing tags where not desired, or configure `pretty` options for indentation).","message":"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.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Familiarize yourself with the object structure conventions (`@` for attributes, `#` for mixed content) described in the documentation to correctly represent desired XML structures.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure your project is configured for TypeScript if leveraging its types, and review compilation errors that may arise from stricter type enforcement. Test thoroughly after upgrading.","message":"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.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Verify custom `typeHandlers` to ensure they align with the updated behavior and documentation, especially regarding usage with bare text/attribute values and the restriction with aliases.","message":"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.","severity":"gotcha","affected_versions":">=5.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use the `parse` method: `js2xmlparser.parse(root, data, options)` (CommonJS) or `import { parse } from 'js2xmlparser'; parse(root, data, options);` (ESM).","cause":"Attempting to call the module directly as a function after v2.0.0, e.g., `js2xmlparser(root, data, options)`.","error":"TypeError: js2xmlparser is not a function"},{"fix":"Switch 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).","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.","error":"ReferenceError: require is not defined"},{"fix":"Adjust options passed to the `parse` method. For example, use `{ selfClosing: false, pretty: { indent: '  ' } }` to override these defaults and regain specific formatting.","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.","error":"The XML output contains unexpected self-closing tags or indentation for multi-line strings."}],"ecosystem":"npm"}