xml-js is a robust JavaScript library designed for converting between XML text and JavaScript objects or JSON text. It is currently stable at version 1.6.11 and receives active maintenance, with recent updates focusing on performance optimizations and new features like custom processing functions. A key differentiator is its ability to maintain the original order of XML elements during conversion to a JavaScript object, avoiding the common practice of merging same-named nodes into arrays by default, which is crucial for preserving document structure. It supports full XML compliance, parsing elements, attributes, texts, comments, CData, DOCTYPEs, XML declarations, and Processing Instructions. The library is also reversible, meaning conversions from XML to JS/JSON and back to XML will preserve the original data. It is known for its minimal dependencies and portability, working in both Node.js and browser environments.
npm install xml-jsVerified import paths — ran on the pinned version, not inferred.
Demonstrates converting an XML string to JavaScript objects in both compact and non-compact modes, then converting these JavaScript objects back into formatted XML strings.
Review existing code that relies on encoded entities remaining encoded after XML to JS conversion. Adapt logic to handle decoded characters directly. The `{sanitize: true}` option should be removed as it has no effect.If indented CData output is required, explicitly set the `{indentCdata: true}` option when calling `js2xml`.Be aware of this change when constructing JavaScript objects for `js2xml`. Both `{a: 'hi'}` and `{a: {_text: 'hi'}}` should generally work, but the simpler form is now supported for convenience and cleaner output.For precise XML structure preservation and reversibility, always use `{ compact: false }` for both `xml2js` and `js2xml`. Use `{ compact: true }` only if you understand and accept the simplified, potentially order-losing, JSON structure.Ensure you are using named imports for `xml2js` (e.g., `import { xml2js } from 'xml-js';`) or importing the entire module as a namespace (e.g., `import * as convert from 'xml-js';` then calling `convert.xml2js(...)`). For CommonJS, use `const { xml2js } = require('xml-js');` or `const convert = require('xml-js');` and then `convert.xml2js(...)`.Always use `{ compact: false }` for both `xml2js` and `js2xml` when precise XML structure, element order, and full reversibility are critical. This will represent elements as an array of objects, preserving their sequence.Consult the documentation for the specific structure generated by `compact: true` versus `compact: false`. When `compact: true`, text nodes are often accessed directly from the element property, or as `_text` property if it's mixed content. For non-compact mode, access the `text` property within an element of `type: 'text'`.
No dependency data recorded yet.