Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

xml-js

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xml-js - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

.editorconfig

17

bin/cli.js

@@ -5,3 +5,3 @@ #!/usr/bin/env node

var fs = require('fs');
var package = require('../package.json');
var project = require('../package.json');
var common = require('../lib/common');

@@ -22,3 +22,3 @@ var xml2json = require('../lib/xml2json');

{arg: 'to-json', type: 'flag', option:'toJason', desc: 'Convert.'},
{arg: 'compact', type: 'flag', option:'compact', desc: 'Compact JSON form (see www.npmjs.com/package/xml-js).'},
{arg: 'compact', type: 'flag', option:'compact', desc: 'Compact JSON form (see explanation in www.npmjs.com/package/xml-js).'},
{arg: 'spaces', type: 'number', option:'spaces', desc: 'Specifies amount of space indentation in the output.'},

@@ -34,4 +34,4 @@ {arg: 'trim', type: 'flag', option:'trim', desc: 'Any whitespaces surrounding texts will be trimmed.'},

{arg: 'no-text', type: 'flag', option:'ignoreText', desc: 'Texts of elements will be ignored.'},
{arg: 'no-cdata', type: 'flag', option:'ignoreCdata', desc: 'Cdata of elements will be ignored.'},
{arg: 'no-doctype', type: 'flag', option:'ignoreDoctype', desc: 'Doctype of elements will be ignored.'},
{arg: 'no-cdata', type: 'flag', option:'ignoreCdata', desc: 'CData of elements will be ignored.'},
{arg: 'no-doctype', type: 'flag', option:'ignoreDoctype', desc: 'DOCTYPE of elements will be ignored.'},
{arg: 'no-comment', type: 'flag', option:'ignoreComment', desc: 'Comments of elements will be ignored.'},

@@ -56,3 +56,3 @@ {arg: 'text-key', type: 'string', option:'textKey', desc: 'To change the default \'text\' key.'},

});
process.stdin.on('end', function () {
process.stdin.on('end', function () {
process.stdout.write(xml2json(stream, {}) + '\n');

@@ -65,4 +65,4 @@ });

if (options.version) {
console.log(package.version);
process.exit(0);
console.log(project.version);
process.exit(0);
} else if (options.help || process.argv.length <= 2 + requiredArgs.length - 1) {

@@ -72,3 +72,2 @@ console.log(common.getCommandLineHelp('xml-js', requiredArgs, optionalArgs));

} else if ('src' in options) {
//console.log('---------------' + fs.statSync(options.src).isFile());
if (fs.statSync(options.src).isFile()) {

@@ -89,2 +88,2 @@ if (options.src.split('.').pop() === 'xml') {

process.exit(1);
}
}

@@ -58,3 +58,3 @@ var common = require('./common');

function writeDoctype (doctype, options) {
return options.ignoreDoctype ? '' : '<!DOCTYPE' + doctype + '>';
return options.ignoreDoctype ? '' : '<!DOCTYPE ' + doctype + '>';
}

@@ -176,29 +176,2 @@

// function writeElementsCompact (element, options, depth, firstLine) {
// var key, xml = '';
// for (key in element) {
// if (element.hasOwnProperty(key)) {
// switch (key) {
// case options.declarationKey: xml += writeDeclaration(element[options.declarationKey], options); break;
// case options.attributesKey: case options.parentKey: break; // skip
// case options.textKey: xml += (options.indentText ? writeIndentation(options, depth, firstLine) : '') + writeText(element, options); break;
// case options.cdataKey: xml += (options.indentCdata ? writeIndentation(options, depth, firstLine) : '') + writeCdata(element, options); break;
// case options.doctypeKey: xml += writeIndentation(options, depth, firstLine) + writeDoctype(element, options); break;
// case options.commentKey: xml += writeIndentation(options, depth, firstLine) + writeComment(element, options); break;
// default:
// if (element[key] instanceof Array) {
// element[key].forEach(function (el) {
// xml += writeIndentation(options, depth, firstLine) + writeElementCompact(el, key, options, depth, hasContentCompact(el, options));
// firstLine = firstLine && !xml;
// });
// } else {
// xml += writeIndentation(options, depth, firstLine) + writeElementCompact(element[key], key, options, depth, hasContentCompact(element[key], options));
// }
// }
// firstLine = firstLine && !xml;
// }
// }
// return xml;
// }
function writeElementsCompact (element, options, depth, firstLine) {

@@ -241,2 +214,2 @@ var i, key, nodes, xml = '';

return xml;
};
};

@@ -53,3 +53,13 @@ var sax = require('sax');

if (options.compact) {
currentElement[options[type + 'Key']] = (currentElement[options[type + 'Key']] ? currentElement[options[type + 'Key']] + '\n' : '') + value;
if (!currentElement[options[type + 'Key']] && options.alwaysArray) {
currentElement[options[type + 'Key']] = [];
}
if (currentElement[options[type + 'Key']] && !(currentElement[options[type + 'Key']] instanceof Array)) {
currentElement[options[type + 'Key']] = [currentElement[options[type + 'Key']]];
}
if (currentElement[options[type + 'Key']] instanceof Array) {
currentElement[options[type + 'Key']].push(value);
} else {
currentElement[options[type + 'Key']] = value;
}
} else {

@@ -117,11 +127,13 @@ if (!currentElement[options.elementsKey]) {

element[options.parentKey] = currentElement;
if (!(name in currentElement)) {
if (!(name in currentElement) && options.alwaysArray) {
currentElement[name] = [];
}
if (currentElement[name] && !(currentElement[name] instanceof Array)) {
currentElement[name] = [currentElement[name]];
}
if (currentElement[name] instanceof Array) {
currentElement[name].push(element);
} else {
currentElement[name] = element;
} else {
if (!(currentElement[name] instanceof Array)) {
currentElement[name] = [currentElement[name]];
}
currentElement[name].push(element);
}
currentElement = element;
} else {

@@ -142,8 +154,7 @@ if (!currentElement[options.elementsKey]) {

currentElement[options.elementsKey].push(element);
currentElement = element;
}
currentElement = element;
}
function onText (text) {
//console.log('currentElement:', currentElement);
if (options.ignoreText) {

@@ -202,2 +213,3 @@ return;

}
doctype = doctype.replace(/^ /, '');
if (options.trim) {

@@ -214,9 +226,9 @@ doctype = doctype.trim();

module.exports = function (xml, userOptions) {
var parser = pureJsParser ? sax.parser(true, {}) : parser = new expat.Parser('UTF-8');
var result = {};
currentElement = result;
options = validateOptions(userOptions);
if (pureJsParser) {

@@ -241,3 +253,3 @@ parser.onopentag = onStartElement;

}
if (pureJsParser) {

@@ -250,3 +262,3 @@ parser.write(xml).close();

}
if (result[options.elementsKey]) {

@@ -258,5 +270,5 @@ var temp = result[options.elementsKey];

}
return result;
};
};
{
"name": "xml-js",
"version": "1.1.0",
"version": "1.2.0",
"description": "A convertor between XML text and Javascript object / JSON text.",

@@ -5,0 +5,0 @@ "repository": {

@@ -29,3 +29,3 @@ ![XML ⇔ JS/JSON](http://nashwaan.github.io/xml-js/images/logo.svg)

* **Maintain Order of Elements**:
Instead of converting `<a/><b/><a/>` to `{a:[{},{}],b:{}}`, I wanted to preserve order of elements by doing this:
Instead of converting `<a/><b/><a/>` to `{a:[{},{}],b:{}}`, I wanted to preserve order of elements by doing this:
`{"elements":[{"type":"element","name":"a"},{"type":"element","name":"b"},{"type":"element","name":"a"}]}`.

@@ -43,3 +43,3 @@

* **Change Property Key Name**:
Usually output of XML attributes are stored in `@attr`, `_atrr`, `$attr`, `$`, or `whatever` in order to avoid conflicting with name of sub-elements.
Usually output of XML attributes are stored in `@attr`, `_atrr`, `$attr`, `$`, or `whatever` 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.

@@ -60,12 +60,12 @@

Thanks to the wonderful efforts by [DenisCarriere](https://github.com/DenisCarriere)
## Compact vs Non-Compact
Most XML to JSON convertors (including online convertors) convert `<a/>` to some compact output like `{"a":{}}`
Most XML to JSON convertors (including online convertors) convert `<a/>` 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 `<a x="1"/><b x="2"/><a x="3"/>` as an example.
Most converters will produce compact like `{a:[{_:{x:"1"}},{_:{x:"3"}}], b:{_:{x:"2"}}}`,
which has merged both `<a>` elements into an array! If you try to convert this back to xml, you will get `<a x="1"/><a x="3"/><b x="2"/>`
which has not preserved the order of elements! This is an inherit limitation in the compact representation
Most converters will produce compact like `{a:[{_:{x:"1"}},{_:{x:"3"}}], b:{_:{x:"2"}}}`,
which has merged both `<a>` elements into an array! If you try to convert this back to xml, you will get `<a x="1"/><a x="3"/><b x="2"/>`
which has not preserved the order of elements! This is an inherit 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).

@@ -75,6 +75,6 @@

Another drawback of compact output is the resultant element can be an object or an array and therefore makes the client code a little awkwards in terms of extra check of object type before processing.
Another drawback of compact output is the resultant element can be an object or an array and therefore makes the client code a little awkwards 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;
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 type and loosing their order.

@@ -100,3 +100,3 @@

var convert = require('xml-js');
var xml =
var xml =
'<?xml version="1.0" encoding="utf-8"?>' +

@@ -160,11 +160,11 @@ '<note importance="high" logged="true">' +

|:----------------------|:--------|:------------|
| `spaces` | `0` | Number of spaces to be used for indenting XML output. Passing characters like `'` &nbsp;&nbsp;&nbsp; `'` or `'\t'` are also accpeted. |
| `spaces` | `0` | Number of spaces to be used for indenting XML output. Passing characters like `' '` or `'\t'` are also accpeted. |
| `compact` | `false` | Whether the *input* object is in compact form or not. |
| `fullTagEmptyElement` | `false` | Whether to produce element without sub-elements as full tag pairs `<a></a>` rather than self closing tag `<a/>`. |
| `indentCdata` | `false` | Whether to write CData in a new line and indent it. Will generate `<a>\n <![CDATA[foo]]></a>` instead of `<a><![CDATA[foo]]></a>`. |
| `indentCdata` | `false` | Whether to write CData in a new line and indent it. Will generate `<a>\n <![CDATA[foo]]></a>` instead of `<a><![CDATA[foo]]></a>`. See [discussion](https://github.com/nashwaan/xml-js/issues/14) |
| `ignoreDeclaration` | `false` | Whether to ignore writing declaration directives of xml. For example, `<?xml?>` will be ignored. |
| `ignoreAttributes` | `false` | Whether to ignore writing attributes of the elements. For example, `x="1"` in `<a x="1"></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. |
| `ignoreDoctype` | `false` | Whether to ignore writing Doctype of the elements. That is, no `<!DOCTYPE ]>` will be generated. |
| `ignoreCdata` | `false` | Whether to ignore writing CData of the elements. That is, no `<![CDATA[ ]]>` will be generated. |
| `ignoreDoctype` | `false` | Whether to ignore writing Doctype of the elements. That is, no `<!DOCTYPE >` will be generated. |
| `ignoreText` | `false` | Whether to ignore writing texts of the elements. For example, `hi` text in `<a>hi</a>` will be ignored. |

@@ -207,3 +207,3 @@

|:--------------------|:--------|:------------|
| `spaces` | `0` | Number of spaces to be used for indenting JSON output. Passing characters like `'` &nbsp;&nbsp;&nbsp; `'` or `'\t'` are also accpeted. |
| `spaces` | `0` | Number of spaces to be used for indenting JSON output. Passing characters like `' '` or `'\t'` are also accpeted. |

@@ -220,3 +220,3 @@ ## Options for Changing Key Names

| `cdataKey` | `"cdata"` 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)* |
| `doctypeKey` | `"doctype"` or `"_doctype"` | Name of the property key which will be used for the doctype. For example, if `doctypeKey: '$doctype'` then output of `<!DOCTYPE foo>` will be `{"$doctype":" foo}` *(in compact form)* |
| `doctypeKey` | `"doctype"` or `"_doctype"` | Name of the property key which will be used for the doctype. For example, if `doctypeKey: '$doctype'` then output of `<!DOCTYPE foo>` will be `{"$doctype":" foo}` *(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)* |

@@ -265,3 +265,3 @@ | `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)* |

```
```

@@ -288,4 +288,4 @@ npm run convert // task 'scripts.convert' will be executed

--no-text Texts of elements will be ignored.
--no-cdata Cdata of elements will be ignored.
--no-doctype Doctype of elements will be ignored.
--no-cdata CData of elements will be ignored.
--no-doctype DOCTYPE of elements will be ignored.
--no-comment Comments of elements will be ignored.

@@ -296,2 +296,3 @@ --trim Any whitespaces surrounding texts will be trimmed.

--native-type Numbers and boolean will be converted (coreced) to native type instead of text.
--always-array Every element will always be an array type (applicable if --compact is set).
--always-children Every element will always contain sub-elements (applicable if --compact is not set).

@@ -328,2 +329,2 @@ --text-key To change the default 'text' key.

[MIT](https://github.com/nashwaan/xml-js/blob/master/LICENSE)
[MIT](https://github.com/nashwaan/xml-js/blob/master/LICENSE)

@@ -45,2 +45,3 @@ export interface ElementCompact {

addParent?: boolean
alwaysArray?: boolean
alwaysChildren?: boolean

@@ -52,2 +53,4 @@ }

compact?: boolean
indentText?: boolean
indentCdata?: boolean
fullTagEmptyElement?: boolean

@@ -82,2 +85,2 @@ }

export function xml2json(xml: string, options?: Options.XML2JS): string;
export function xml2js(xml: string, options?: Options.XML2JS): any;
export function xml2js(xml: string, options?: Options.XML2JS): any;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc