Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
The xml-js npm package is a utility that provides methods to convert between XML and JSON format. It can also convert between XML and JavaScript objects. This package is useful when working with XML data in JavaScript environments where JSON is more easily manipulated.
XML to JSON Conversion
Converts an XML string to a JSON string. The options object can be used to customize the output.
const convert = require('xml-js');
const xml = '<note><to>User</to><from>Library</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>';
const options = {compact: true, ignoreComment: true, spaces: 4};
const result = convert.xml2json(xml, options);
console.log(result);
JSON to XML Conversion
Converts a JSON string to an XML string. The options object can be used to customize the output.
const convert = require('xml-js');
const json = '{"note":{"to":"User","from":"Library","heading":"Reminder","body":"Don't forget me this weekend!"}}';
const options = {compact: true, ignoreComment: true, spaces: 4};
const result = convert.json2xml(json, options);
console.log(result);
XML to JavaScript Object
Converts an XML string to a JavaScript object. The options object can be used to customize the output.
const convert = require('xml-js');
const xml = '<note><to>User</to><from>Library</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>';
const result = convert.xml2js(xml, {compact: true, spaces: 4});
console.log(result);
JavaScript Object to XML
Converts a JavaScript object to an XML string. The options object can be used to customize the output.
const convert = require('xml-js');
const jsObject = {
note: {
to: 'User',
from: 'Library',
heading: 'Reminder',
body: 'Don't forget me this weekend!'
}
};
const options = {compact: true, spaces: 4};
const result = convert.js2xml(jsObject, options);
console.log(result);
A very fast XML parser and validator with a similar feature set to xml-js. It can parse XML to a JavaScript object/JSON and vice versa. It is known for its performance and, unlike xml-js, includes XML validation.
Another popular XML to JavaScript converter that provides a similar conversion functionality as xml-js. It includes options to customize the parser behavior and, like xml-js, it does not validate the XML.
This package allows for building XML documents from JavaScript objects, focusing on the creation of XML content. It is more specialized in generating XML and does not provide parsing from XML to JavaScript objects.
Convert XML text to Javascript object (and vice versa) or to JSON text (and vice versa):
npm install --save xml-js
Quick start:
var convert = require('xml-js');
var xml =
'<?xml version="1.0" encoding="utf-8"?>' + '\n' +
'<note importance="high" logged="true">' + '\n' +
' <title>Happy</title>' + '\n' +
' <todo>Work</todo>' + '\n' +
' <todo>Play</todo>' + '\n' +
'</note>';
var result = convert.xml2json(xml, {compact: true});
console.log(result);
Output object as compact version. options = {compact: true}
{
"_declaration": {
"_attributes": {
"version": "1.0",
"encoding": "utf-8"
}
},
"note": {
"_attributes": {
"importance": "high",
"logged": "true"
},
"title": {
"_text": "Happy"
},
"todo": [
{
"_text": "Work"
},
{
"_text": "Play"
}
]
}
}
Output object as detailed version. options = {compact: false}
{
"declaration": {
"attributes": {
"version": "1.0",
"encoding": "utf-8"
}
},
"elements": [
{
"type": "element",
"name": "note",
"attributes": {
"importance": "high",
"logged": "true"
},
"elements": [
{
"type": "element",
"name": "title",
"attributes": {},
"elements": [
{
"type": "text",
"text": "Happy"
}
]
},
{
"type": "element",
"name": "todo",
"attributes": {},
"elements": [
{
"type": "text",
"text": "Work"
}
]
},
{
"type": "element",
"name": "todo",
"attributes": {},
"elements": [
{
"type": "text",
"text": "Play"
}
]
}
]
}
]
}
There are many XML to JavaScript/JSON converters out there, but could not satisfy the following requirements:
var convert = require('xml-js');
var json = require('fs').readFileSync('test.json');
var options = {ignoreText: true, spaces: 4};
var result = convert.json2xml(json, options);
console.log(result);
Option | Default | Description |
---|---|---|
ignoreDeclaration | false | Whether to ignore writing declaration directives of xml. For example, <?xml?> will be ignored. |
ignoreAttributes | false | Whether to ignore writing texts of the elements. For example, x="1" in <a x="1"></a> will be ignored |
ignoreText | false | Whether to ignore writing texts of the elements. For example, hi text in <a>hi</a> 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 <![CDATA[ ]]> will be generated. |
spaces | 0 | Number of spaces to be used for indenting xml output. |
fromCompact | false | whether the source object is in compact form. |
fullTagEmptyElement | false | Whether to produce element without sub-elements as full tag pairs <a></a> rather than self closing tag </a> . |
var convert = require('xml-js');
var xml = require('fs').readFileSync('test.xml');
var options = {ignoreText: true, emptyChildren: true};
var result = convert.xml2js(xml, options); // or convert.xml2json(xml, options)
console.log(result);
Option | Default | Description |
---|---|---|
ignoreDeclaration | false | Whether to ignore writing declaration property. That is, no declaration property will be generated. |
ignoreAttributes | false | Whether to ignore writing attributes of elements.That is, no attributes property will be generated. |
ignoreText | false | Whether to ignore writing texts of the elements. That is, no text property will be generated. |
ignoreComment | false | Whether to ignore writing comments of the elements. That is, no comment will be generated. |
ignoreCdata | false | Whether to ignore writing CData of the elements. That is, no cdata property will be generated. |
compact | false | Whether to produce detailed object or compact object. |
emptyChildren | false | Whether to always generate elements property even when there are no actual sub elements. |
addParent | false | Whether to add parent property in each element object that points to parent object. |
trim | false | Whether to trim white space characters that may exist before and after the 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 |
sanitize | false | Whether to replace & < > " ' with & < > " ' respectively in the resultant text. |
declarationKey | "declaration" or "declaration" | Name of the property key which will be used for the declaration. For example, if declarationKey: '$declaration' then output of <?xml?> will be {"$declaration":{}} (in compact form) |
attributesKey | "attributes" or "_attributes" | Name of the property key which will be used for the attributes. For example, if attributesKey: '$attributes' then output of <a x="hello"/> will be {"a":{$attributes:{"x":"hello"}}} (in compact form) |
textKey | "text" or "_text" | Name of the property key which will be used for the text. For example, if textKey: '$text' then output of <a>hi</a> will be {"a":{"$text":"Hi"}} (in compact form) |
commentKey | "comment" or "_comment" | Name of the property key which will be used for the comment. For example, if commentKey: '$comment' then output of <!--note--> will be {"$comment":"note"} (in compact form) |
cdataKey | "cdat" or "_cdata" | Name of the property key which will be used for the cdata. For example, if cdataKey: '$cdata' then output of <![CDATA[1 is < 2]]> will be {"$cdata":"1 is < 2"} (in compact form) |
parentKey | "parent" or "_parent" | Name of the property key which will be used for the parent. For example, if parentKey: '$parent' then output of <a></b></a> will be {"a":{"b":{$parent:_points_to_a}}} (in compact form) |
typeKey | "type" | Name of the property key which will be used for the type. For example, if typeKey: '$type' then output of <a></a> will be {"elements":[{"$type":"element","name":"a","attributes":{}}]} (in non-compact form) |
nameKey | "name" | Name of the property key which will be used for the name. For example, if nameKey: '$name' then output of <a></a> will be {"elements":[{"type":"element","$name":"a","attributes":{}}]} (in non-compact form) |
elementsKey | "elements" | Name of the property key which will be used for the elements. For example, if elementsKey: '$elements' then output of <a></a> will be {"$elements":[{"type":"element","name":"a","attributes":{}}]} (in non-compact form) |
To perform tests on this project:
cd node_modules/xml-js/test
npm test
Use this link to report an issue or bug. Please include a sample code or Jasmine test spec where the code is failing.
If you want to add a feature or fix a bug, please fork the repository and make the changes in your fork. Add tests to ensure your code is working properly, then submit a pull request.
FAQs
A convertor between XML text and Javascript object / JSON text.
The npm package xml-js receives a total of 1,247,754 weekly downloads. As such, xml-js popularity was classified as popular.
We found that xml-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.