Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
X

xml-js

> DevOps
Open source

Converter utility between XML text and Javascript object / JSON text.

1.3K stars0 likes1 views
WebsiteGitHub

About

Converter utility between XML text and Javascript object / JSON text.

Convert XML text to Javascript object / JSON text (and vice versa).

Synopsis

Features

  • Maintain Order of Elements: Most libraries will convert `` to {a:[{},{}],b:{}} which merges any node of same name into an array. This library can create the following to preserve the order of elements: {"elements":[{"type":"element","name":"a"},{"type":"element","name":"b"},{"type":"element","name":"a"}]}.

This is very important and it is the main reason why this library was created. Read also Compact vs Non-Compact for more info.

  • Fully XML Compliant: Can parse: elements, attributes, texts, comments, CData, DOCTYPE, XML declarations, and Processing Instructions.

  • Reversible: Whether converting xml→json or json→xml, the result can be converted back to its original form.

  • Minimal Dependencies: This library depends only on one external npm module.

  • Change Property Key Name: Usually output of XML attributes are stored in @attr, _atrr, $attr or $ in order to avoid conflicting with name of sub-elements. This library store them in attributes, but most importantly, you can change this to whatever you like.

  • Support Upwards Traversal: By setting {addParent: true} option, an extra property named parent will be generated along each element so that its parent can be referenced. Therefore, anywhere during the traversal of an element, its children and its parent can be easily accessed.

  • Support Command Line: To quickly convert xml or json files, this module can be installed globally or locally (i.e. use it as script in package.json).

  • Customize Processing using Callback Hooks: Custom functions can be supplied to do additional processing for different parts of xml or json (like cdata, comments, elements, attributes ...etc).

  • Portable Code: Written purely in JavaScript which means it can be used in Node environment and browser environment (via bundlers like browserify/JSPM/Webpack).

  • Typings Info Included: Support type checking and code suggestion via intellisense. Thanks to the wonderful efforts by DenisCarriere.

Compact vs Non-Compact

Most XML to JSON converters (including online converters) convert `` to some compact output like {"a":{}} instead of non-compact output like {"elements":[{"type":"element","name":"a"}]}.

While compact output might work in most situations, there are cases when elements of different names are mixed inside a parent element. Lets use as an example. Most converters will produce compact output like this `{a:[{_:{x:"1"}},{_:{x:"3"}}], b:{_:{x:"2"}}}`, which has merged both elements into an array. If you try to convert this back to xml, you will get `` which has not preserved the order of elements!

The reason behind this behavior is due to the inherent limitation in the compact representation. Because output like {a:{_:{x:"1"}}, b:{_:{x:"2"}}, a:{_:{x:"3"}}} is illegal (same property name a should not appear twice in an object). This leaves no option but to use array {a:[{_:{x:"1"}},{_:{x:"3"}}].

The non-compact output, which is supported by this library, will produce more information and always guarantees the order of the elements as they appeared in the XML file.

Another drawback of compact output is the resultant element can be an object or an array and therefore makes the client code a little awkward in terms of the extra check needed on object type before processing.

NOTE: Although non-compact output is more accurate representation of original XML than compact version, the non-compact version is verbose and consumes more space. This library provides both options. Use {compact: false} if you are not sure because it preserves everything; otherwise use {compact: true} if you want to save space and you don't care about mixing elements of same name and losing their order.

Tip: You can reduce the output size by using shorter key names.

Usage

Installation

bash
npm install --save xml-js

You can also install it globally to use it as a command line convertor (see Command Line).

bash
npm install --global xml-js

Quick start

javascript
var convert = require('xml-js');
var xml =
'' +
'' +
'    Happy' +
'    Work' +
'    Play' +
'';
var result1 = convert.xml2json(xml, {compact: true, spaces: 4});
var result2 = convert.xml2json(xml, {compact: false, spaces: 4});
console.log(result1, '\n', result2);

To see the result of this code, see the output above in Synopsis section.

Or run and edit this code live in the browser.

Sample Conversions

XML JS/JSON compact JS/JSON non-compact
`` {"a":{}} {"elements":[{"type":"element","name":"a"}]}
`` {"a":{},"b":{}} {"elements":[{"type":"element","name":"a"},{"type":"element","name":"b"}]}
`` {"a":{"b":{}}} {"elements":[{"type":"element","name":"a","elements":[{"type":"element","name":"b"}]}]}
Hi {"a":{"_text":" Hi "}} {"elements":[{"type":"element","name":"a","elements":[{"type":"text","text":" Hi "}]}]}
`` {"a":{"_attributes":{"x":"1.234","y":"It's"}}} {"elements":[{"type":"element","name":"a","attributes":{"x":"1.234","y":"It's"}}]}
`` {"_declaration":{}} {"declaration":{}}
`` {"_instruction":{"go":"there"}} {"elements":[{"type":"instruction","name":"go","instruction":"there"}]}
`` {"_declaration":{"_attributes":{"version":"1.0","encoding":"utf-8"}}} {"declaration":{"attributes":{"version":"1.0","encoding":"utf-8"}}}
`` {"_comment":"Hello, World!"} {"elements":[{"type":"comment","comment":"Hello, World!"}]}
]]> {"_cdata":""} {"elements":[{"type":"cdata","cdata":""}]}

API Reference

This library provides 4 functions: js2xml(), json2xml(), xml2js(), and xml2json(). Here are the usages for each one (see more details in the following sections):

javascript
var convert = require('xml-js');
result = convert.js2xml(js, options);     // to convert javascript object to xml text
result = convert.json2xml(json, options); // to convert json text to xml text
result = convert.xml2js(xml, options);    // to convert xml text to javascript object
result = convert.xml2json(xml, options);  // to convert xml text to json text

Convert JS object / JSON → XML

To convert JavaScript object to XML text, use js2xml(). To convert JSON text to XML text, use json2xml().

javascript
var convert = require('xml-js');
var json = require('fs').readFileSync('test.json', 'utf8');
var options = {compact: true, ignoreComment: true, spaces: 4};
var result = convert.json2xml(json, options);
console.log(result);

Options for Converting JS object / JSON → XML

The below options are applicable for both js2xml() and json2xml() functions.

Option Default Description
spaces 0 Number of spaces to be used for indenting XML output. Passing characters like ' ' or '\t' are also accepted.
compact false Whether the input object is in compact form or not. By default, input is expected to be in non-compact form.
IMPORTANT: Remeber to set this option compact: true if you are supplying normal json (which is likely equivalent to compact form). Otherwise, the function assumes your json input is non-compact form and you will not get a result if it is not in that form. See Synopsis to know the difference between the two json forms
fullTagEmptyElement false Whether to produce element without sub-elements as full tag pairs rather than self closing tag.
indentCdata false Whether to write CData in a new line and indent it. Will generate \n instead of ``. See discussion
indentAttributes false Whether to print attributes across multiple lines and indent them (when spaces is not 0). See example.
ignoreDeclaration false Whether to ignore writing declaration directives of xml. For example, `` will be ignored.
ignoreInstruction false Whether to ignore writing processing instruction of xml. For example, `` will be ignored.
ignoreAttributes false Whether to ignore writing attributes of the elements. For example, x="1" in `` will be ignored
ignoreComment false Whether to ignore writing comments of the elements. That is, no `` will be generated.
ignoreCdata false Whether to ignore writing CData of the elements. That is, no `` will be generated.
ignoreDoctype false Whether to ignore writing Doctype of the elements. That is, no `` will be generated.
ignoreText false Whether to ignore writing texts of the elements. For example, hi text in hi will be ignored.

Convert XML → JS object / JSON

To convert XML text to JavaScript object, use xml2js(). To convert XML text to JSON text, use xml2json().

javascript
var convert = require('xml-js');
var xml = require('fs').readFileSync('test.xml', 'utf8');
var options = {ignoreComment: true, alwaysChildren: true};
var result = convert.xml2js(xml, options); // or convert.xml2json(xml, options)
console.log(result);

Options for Converting XML → JS object / JSON

The below options are applicable for both xml2js() and xml2json() functions.

Option Default Description
compact false Whether to produce detailed object or compact object.
trim false Whether to trim whitespace characters that may exist before and after the text.
sanitize (Deprecated) false Whether to replace & `` with &amp; &lt; &gt; respectively, in the resultant text.
nativeType false Whether to attempt converting text of numerals or of boolean values to native type. For example, "123" will be 123 and "true" will be true
nativeTypeAttributes false Whether to attempt converting attributes of numerals or of boolean values to native type. See also nativeType above.
addParent false Whether to add parent property in each element object that points to parent object.
alwaysArray false Whether to always put sub element, even if it is one only, as an item inside an array. `` will be a:[{b:[{}]}] rather than a:{b:{}} (applicable for compact output only). If the passed value is an array, only elements with names in the passed array are always made arrays.
alwaysChildren false Whether to always generate elements property even when there are no actual sub elements. `` will be {"elements":[{"type":"element","name":"a","elements":[]}]} rather than {"elements":[{"type":"element","name":"a"}]} (applicable for non-compact output).
instructionHasAttributes false Whether to parse contents of Processing Instruction as attributes or not. `` will be {"_instruction":{"go":{"_attributes":{"to":"there"}}}} rather than {"_instruction":{"go":"to=\"there\""}}. See discussion.
ignoreDeclaration false Whether to ignore parsing declaration property. That is, no declaration property will be generated.
ignoreInstruction false Whether to ignore parsing processing instruction property. That is, no instruction property will be generated.
ignoreAttributes false Whether to ignore p

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScriptcdataconvert-jsconvertorjavascript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
CategoryDevOps
PricingOpen source

> Related tools

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理