Registry / serialization / js2xmlparser

js2xmlparser

JSON →
library5.0.0jsnpmunverified

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.

npm install js2xmlparser
INSTALL
IMPORT
SIG · JS2XMLPARSER
J
js2xmlparser
serializationjavascriptv5.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse
import { parse } from 'js2xmlparser';
const js2xmlparser = require('js2xmlparser'); js2xmlparser(root, data, options);
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.
js2xmlparser (CommonJS)
const js2xmlparser = require('js2xmlparser');
import * as js2xmlparser from 'js2xmlparser';
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.
Options interface
import type { Options } from 'js2xmlparser';
import { Options } from 'js2xmlparser';
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.

Demonstrates parsing a complex JavaScript object into XML, showcasing attribute handling, nested objects, array elements, and mixed content using the `parse` function.

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);
Debug
Known issues
breakingThe primary API for parsing changed significantly in v2.0.0. Direct invocation of the module (`js2xmlparser(root, data, options)`) was removed.
fix
Migrate all calls to `js2xmlparser.parse(root, data, options)` or `parse(root, data, options)` if using named imports.
affects: >=2.0.0
breakingOutput 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.
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).
affects: >=4.0.0
gotchaTo 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.
fix
Familiarize yourself with the object structure conventions (`@` for attributes, `#` for mixed content) described in the documentation to correctly represent desired XML structures.
affects: >=1.0.0
breakingThe 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.
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.
affects: >=2.0.0
gotchaAs 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.
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.
affects: >=5.0.0
Errors
Common errors & fixes
TypeError: js2xmlparser is not a function
Attempting to call the module directly as a function after v2.0.0, e.g., `js2xmlparser(root, data, options)`.
fix
Use the `parse` method: `js2xmlparser.parse(root, data, options)` (CommonJS) or `import { parse } from 'js2xmlparser'; parse(root, data, options);` (ESM).
ReferenceError: require is not defined
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.
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).
The XML output contains unexpected self-closing tags or indentation for multi-line strings.
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.
fix
Adjust options passed to the `parse` method. For example, use `{ selfClosing: false, pretty: { indent: '  ' } }` to override these defaults and regain specific formatting.
Upgrade
Version history
5.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources