Comparing version 0.8.0 to 0.8.1
{ | ||
"name": "xml-js", | ||
"version": "0.8.0", | ||
"version": "0.8.1", | ||
"description": "A convertor between XML text and Javascript object / JSON text.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
174
README.md
@@ -23,6 +23,18 @@ ![Alt text](/logo.svg?raw=true "Logo") | ||
## Motivation | ||
There are many XML to JavaScript/JSON converters out there, but could not satisfy the following requirements: | ||
* **Maintain order of sub-nodes in xml**: | ||
I wanted `<a/><b/><a/>` to give output as `{"elements":[{"type":"element","name":"a"},{"type":"element","name":"b"},{"type":"element","name":"a"}]}` instead of `{a:"",b:""}`. | ||
* Fully XML Compliant | ||
* Portable (this is default behavior: only Javascript code, slower execution) | ||
* Fast (if required; will compile on VC++) | ||
* Support streaming | ||
* Support command line | ||
## Installation | ||
```bash | ||
npm install --save xml-js | ||
npm install xml-js | ||
``` | ||
@@ -37,108 +49,18 @@ | ||
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' + | ||
'<?xml version="1.0" encoding="utf-8"?>' + | ||
'<note importance="high" logged="true">' + | ||
' <title>Happy</title>' + | ||
' <todo>Work</todo>' + | ||
' <todo>Play</todo>' + | ||
'</note>'; | ||
var result = convert.xml2json(xml, {compact: true}); | ||
console.log(result); | ||
var result1 = convert.xml2json(xml, {compact: true}); | ||
var result2 = convert.xml2json(xml, {compact: false}); | ||
console.log(result1, '\n', result2); | ||
``` | ||
Output object as compact version. `options = {compact: true}` | ||
```json | ||
{ | ||
"_declaration": { | ||
"_attributes": { | ||
"version": "1.0", | ||
"encoding": "utf-8" | ||
} | ||
}, | ||
"note": { | ||
"_attributes": { | ||
"importance": "high", | ||
"logged": "true" | ||
}, | ||
"title": { | ||
"_text": "Happy" | ||
}, | ||
"todo": [ | ||
{ | ||
"_text": "Work" | ||
}, | ||
{ | ||
"_text": "Play" | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
| result1 (compact) | result2 (non-compact) | | ||
|:------------------|:----------------------| | ||
| <code>{<br> "_declaration": {<br> "_attributes": {<br> "version": "1.0",<br> "encoding": "utf-8"<br> }<br> },<br> "note": {<br> "_attributes": {<br> "importance": "high",<br> "logged": "true"<br> },<br> "title": {<br> "_text": "Happy"<br> },<br> "todo": [<br> {<br> "_text": "Work"<br> },<br> {<br> "_text": "Play"<br> }<br> ]<br> }<br>}</code>|<code>{<br> "declaration": {<br> "attributes": {<br> "version": "1.0",<br> "encoding": "utf-8"<br> }<br> },<br> "elements": [<br> {<br> "type": "element",<br> "name": "note",<br> "attributes": {<br> "importance": "high",<br> "logged": "true"<br> },<br> "elements": [<br> {<br> "type": "element",<br> "name": "title",<br> "elements": [<br> {<br> "type": "text",<br> "text": "Happy"<br> }<br> ]<br> },<br> {<br> "type": "element",<br> "name": "todo",<br> "elements": [<br> {<br> "type": "text",<br> "text": "Work"<br> }<br> ]<br> },<br> {<br> "type": "element",<br> "name": "todo",<br> "elements": [<br> {<br> "type": "text",<br> "text": "Play"<br> }<br> ]<br> }<br> ]<br> }<br> ]<br>}</code>| | ||
Output object as detailed version. `options = {compact: false}` | ||
```json | ||
{ | ||
"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" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
``` | ||
## Motivation | ||
There are many XML to JavaScript/JSON converters out there, but could not satisfy the following requirements: | ||
* Full XML Compliant | ||
* Portable (if required: only JS) | ||
* Fast (if required; will compile on VC++) | ||
* Support streaming | ||
* Support command line | ||
## API Reference | ||
@@ -157,11 +79,11 @@ | ||
| 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. | | ||
|:----------------------|:--------|:------------| | ||
| `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>`. | | ||
| `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>`. | | ||
@@ -179,14 +101,18 @@ ### Convert XML to JS object / JSON | ||
| 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. | | ||
|:--------------------|:--------|:------------| | ||
| `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. | | ||
To change default key names in the output object, use the following options: | ||
| Option | Default | Description | ||
|:--------------------|:--------|:------------| | ||
| `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)* | | ||
@@ -207,5 +133,7 @@ | `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)* | | ||
``` | ||
cd node_modules/xml-js/test | ||
cd node_modules/xml-js | ||
npm install | ||
npm test | ||
``` | ||
For live testing, use `npm start` instead of `npm test`. | ||
@@ -212,0 +140,0 @@ ## Contributions |
@@ -10,3 +10,4 @@ /*jslint node:true */ | ||
'</note>'; | ||
var result = convert.xml2json(xml, {compact: true}); | ||
console.log(result); | ||
var result1 = convert.xml2json(xml, {compact: true}); | ||
var result2 = convert.xml2json(xml, {compact: false}); | ||
console.log(result1, '\n', result2); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
7
1354613
51
22350
147