Socket
Socket
Sign inDemoInstall

js2xmlparser

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js2xmlparser - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

lib/formatter.d.ts

33

CHANGES.md

@@ -0,1 +1,34 @@

## 2.0.0 ##
* Re-write in TypeScript
* Re-write to use xmlcreate (greatly simplifies module source)
* Added support for the ECMAScript 2015 Map and Set objects
* New method of calling module:
```javascript
var js2xmlparser = require("js2xmlparser");
var root = "root";
var data = { hello: "world" };
var options = {};
// old method (no longer works):
// js2xmlparser(root, data, options);
// new method:
js2xmlparser.parse(root, data, options);
```
* New options and changes to functionality of some existing options:
* `declaration` contains additional options
* `attributeString` has additional functionality
* `valueString` has additional functionality
* The functionality provided by `prettyPrinting` is now provided by the new
`format` option, which contains additional options
* `arrayMap` is now `wrapHandlers` to reflect the fact that wrapping is
provided for both arrays and ES2015 sets
* `convertMap` is now `typeHandlers` to match the name change to `arrayMap`
* The functionality provided by `useCDATA` is now provided by the new
`cdataInvalidChars` and `cdataKeys` options, which also provide additional
functionality
* Added support for document type definitions using the `dtd` option
## 1.0.0 ##

@@ -2,0 +35,0 @@

10

NOTICE.md
js2xmlparser
Copyright (C) 2012 Michael Kourlas and other contributors
Copyright (C) 2016 Michael Kourlas
***
This library includes the following software:
xmlcreate
Copyright (C) 2016 Michael Kourlas
Licensed under the Apache License version 2.0
{
"name": "js2xmlparser",
"version": "2.0.0",
"description": "Parses JavaScript objects into XML",

@@ -7,2 +8,3 @@ "keywords": [

"converter",
"javascript",
"js",

@@ -16,6 +18,18 @@ "json",

],
"homepage": "http://www.kourlas.net",
"version": "1.0.0",
"author": "Michael Kourlas <michael@kourlas.net>",
"main": "./lib/js2xmlparser.js",
"license": "Apache-2.0",
"author": {
"name": "Michael Kourlas",
"email": "michael@kourlas.net"
},
"files": [
"lib",
"CHANGES.md",
"LICENSE.md",
"NOTICE.md",
"npm-shrinkwrap.json",
"package.json",
"README.md"
],
"main": "./lib/main.js",
"typings": "./lib/main",
"repository": {

@@ -25,7 +39,21 @@ "type": "git",

},
"dependencies": {
"xmlcreate": "^0.1.1"
},
"devDependencies": {
"mocha": "*",
"should": "*"
},
"license": "Apache-2.0"
"chai": "^3.5.0",
"es6-shim": "^0.35.1",
"gulp": "^3.9.1",
"gulp-filter": "^4.0.0",
"gulp-mocha": "^3.0.1",
"gulp-sourcemaps": "^1.6.0",
"gulp-tslint": "^6.1.1",
"gulp-typedoc": "^2.0.0",
"gulp-typescript": "^2.13.6",
"merge2": "^1.0.2",
"mocha": "^3.0.2",
"tslint": "^3.15.1",
"typedoc": "^0.4.5",
"typings": "^1.3.3"
}
}

332

README.md
# js2xmlparser #
[![Build Status](https://travis-ci.org/michaelkourlas/node-js2xmlparser.svg?branch=master)](https://travis-ci.org/michaelkourlas/node-js2xmlparser)
## Overview ##

@@ -9,264 +11,154 @@

Since XML is a data-interchange format, js2xmlparser is designed primarily for JSON-type objects, arrays and primitive
data types, like many of the other JavaScript to XML parsers currently available for Node.js.
Since XML is a data-interchange format, js2xmlparser is designed primarily for
JSON-type objects, arrays and primitive data types, like many of the other
JavaScript to XML parsers currently available for Node.js.
However, js2xmlparser is capable of parsing any object, including native JavaScript objects such as Date and RegExp, by
taking advantage of each object's toString function. Functions are a special case where the return value of the function
itself is used instead of the toString function, if available.
However, js2xmlparser is capable of parsing any object, including native
JavaScript objects such as `Date` and `RegExp`, by taking advantage of each
object's `toString` function or, if this function does not exist, the `String`
constructor.
js2xmlparser also has support for the new `Map` and `Set` objects introduced in
ECMAScript 2015, treating them as JSON-type objects and arrays respectively.
Support for `Map`s is necessary to generate XML with elements in a specific
order, since JSON-type objects do not guarantee insertion order. `Map` keys are
always converted to strings using the method described above.
js2xmlparser also supports a number of constructs unique to XML:
* attributes (through a unique attribute property in objects)
* mixed content (through a unique value property in objects)
* attributes (through an attribute property in objects)
* mixed content (through value properties in objects)
* multiple elements with the same name (through arrays)
js2xmlparser can also pretty-print the XML it outputs with the option of customizing the indent string.
js2xmlparser can also pretty-print the XML it outputs.
## Installation ##
The easiest way to install js2xmlparser is to use npm: `npm install js2xmlparser`.
The easiest way to install js2xmlparser is using npm:
Alternatively, you may download the source from GitHub and copy it to a folder named "js2xmlparser" within your
"node_modules" directory.
```
npm install js2xmlparser
```
## Usage ##
You can also build js2xmlparser from source using gulp:
The js2xmlparser module contains one function which takes the following arguments:
```
git clone https://github.com/michaelkourlas/node-js2xmlparser.git
npm install
typings install
gulp
```
* `root` - the XML root element's name (string, mandatory)
* `data` - the data to be converted to XML; while the data object can contain arrays, it cannot itself be an array
unless the root element is listed in the arrayMap option (object or JSON string, mandatory)
* `options` - module options (object, optional)
* `declaration` - XML declaration options (object, optional)
* `include` - specifies whether an XML declaration should be included (boolean, optional, default: true)
* `encoding` - value of XML encoding attribute in declaration; a value of null represents no encoding attribute
(string, optional, default: "UTF-8")
* `attributeString` - the name of the property representing an element's attributes; note that any property with a
name equal to the attribute string is ignored except in the context of XML attributes (string, optional, default:
"@")
* `valueString` - the name of the property representing an element's value; note that any property with a name equal
to the value string is ignored except in the context of supplying a value for a tag containing attributes (string,
optional, default: "#")
* `aliasString` - the name of the property representing an element's alias; the name of the containing element will
be replaced with the alias (string, optional, default: "=")
* `prettyPrinting` - pretty-printing options (object, optional)
* `enabled` - specifies whether pretty-printing is enabled (boolean, optional, default: true)
* `indentString` - indent string (string, optional, default: "\t")
* `convertMap` - maps object types (as given by the `Object.prototype.toString.call` method) to functions to convert
those objects to a particular string representation; `*` can be used as a wildcard for all types of objects
(object, optional, default: {})
* `arrayMap` - maps the names of arrays to the names of array elements. If an array name is listed in the map,
the element name will be used to wrap the array and each element will be named based on the mapped name
(object, optional, default: {})
* `useCDATA` - specifies whether strings should be enclosed in CDATA tags; otherwise, illegal XML characters will
be escaped (boolean, optional, default: false)
You'll need to install gulp and typings first if you don't have them:
## Examples ##
```
npm install -g gulp
npm install -g typings
```
The following example illustrates the basic usage of js2xmlparser:
You can then copy the folder into your node_modules directory.
var js2xmlparser = require("js2xmlparser");
The `default` target will build the production variant of js2xmlparser, run all
tests, and build the documentation.
var data = {
"firstName": "John",
"lastName": "Smith"
};
You can build the production variant without running tests using the target
`prod`. You can also build the development version using the target `dev`. At
the moment, the only difference between the two is that the development version
includes source maps.
console.log(js2xmlparser("person", data));
## Usage ##
> <?xml version="1.0" encoding="UTF-8"?>
> <person>
> <firstName>John</firstName>
> <lastName>Smith</lastName>
> </person>
The documentation for the current version is available [here](http://www.kourlas.com/node-js2xmlparser/docs/2.0.0/).
This is a more complex example that builds on the first:
You can also build the documentation using gulp:
var js2xmlparser = require("js2xmlparser");
```
gulp docs
```
var data = {
## Examples ##
The following example illustrates the basic usage of js2xmlparser:
```javascript
var js2xmlparser = require("js2xmlparser");
var obj = {
"firstName": "John",
"lastName": "Smith",
"dateOfBirth": new Date(1964, 7, 26),
"address": {
"@": {
"type": "individual"
"type": "home"
},
"firstName": "John",
"lastName": "Smith",
"dateOfBirth": new Date(1964, 7, 26),
"address": {
"streetAddress": "3212 22nd St",
"city": "Chicago",
"state": "Illinois",
"zip": 10000
},
"phone": [
{
"@": {
"type": "home"
},
"streetAddress": "3212 22nd St",
"city": "Chicago",
"state": "Illinois",
"zip": 10000
"#": "123-555-4567"
},
"phone": [
{
"@": {
"type": "home"
},
"#": "123-555-4567"
{
"@": {
"type": "cell"
},
{
"@": {
"type": "work"
},
"#": "789-555-4567"
},
{
"@": {
"type": "cell"
},
"#": "456-555-7890"
}
],
"email": function() {
return "john@smith.com";
"#": "890-555-1234"
},
"notes": "John's profile is not complete."
};
console.log(js2xmlparser("person", data));
> <?xml version="1.0" encoding="UTF-8"?>
> <person type="individual">
> <firstName>John</firstName>
> <lastName>Smith</lastName>
> <dateOfBirth>Wed Aug 26 1964 00:00:00 GMT-0400 (Eastern Daylight Time)</dateOfBirth>
> <address type="home">
> <streetAddress>3212 22nd St</streetAddress>
> <city>Chicago</city>
> <state>Illinois</state>
> <zip>10000</zip>
> </address>
> <phone type="home">123-555-4567</phone>
> <phone type="work">789-555-4567</telephone>
> <phone type="cell">456-555-7890</phone>
> <email>john@smith.com</email>
> <notes>John&apos;s profile is not complete.</notes>
> </person>
This example uses the alias string feature:
var js2xmlparser = require("js2xmlparser");
var data = {
"telephone": [
"123-555-4567",
{
"#": "789-555-4567",
"=": "fax"
{
"@": {
"type": "work"
},
"456-555-7890"
]
};
console.log(js2xmlparser("person", data));
> <?xml version="1.0" encoding="UTF-8"?>
> <person>
> <telephone>123-555-4567</telephone>
> <fax>789-555-4567</fax>
> <telephone>456-555-7890</telephone>
> </person>
This example uses the convert map feature:
var js2xmlparser = require("js2xmlparser");
var data = {
"email": function() {
return "john@smith.com";
},
"dateOfBirth": new Date(1964, 7, 26)
}
var options = {
convertMap: {
"[object Date]": function(date) {
return date.toISOString();
},
"[object Function]": function(func) {
return func.toString();
}
"#": "567-555-8901"
}
};
],
"email": "john@smith.com"
};
console.log(js2xmlparser("person", data, options));
console.log(js2xmlparser.parse("person", obj));
```
> <?xml version="1.0" encoding="UTF-8"?>
> <person>
> <email>function () {
> return &quot;john@smith.com&quot;;
> }</email>
> <dateOfBirth>1964-08-26T05:00:00.000Z</dateOfBirth>
> </person>
This example produces the following XML:
This example uses the array map feature:
```xml
<?xml version='1.0'?>
<person>
<firstName>John</firstName>
<lastName>Smith</lastName>
<dateOfBirth>Wed Aug 26 1964 00:00:00 GMT-0400 (Eastern Summer Time)</dateOfBirth>
<address type='home'>
<streetAddress>3212 22nd St</streetAddress>
<city>Chicago</city>
<state>Illinois</state>
<zip>10000</zip>
</address>
<phone type='home'>123-555-4567</phone>
<phone type='cell'>890-555-1234</phone>
<phone type='work'>567-555-8901</phone>
<email>john@smith.com</email>
</person>
```
var js2xmlparser = require("js2xmlparser");
Additional examples can be found in examples/example.js.
var data = {
"firstName": "John",
"lastName": "Smith",
"nicknames": [
"Johnny",
"Jon",
"Jack"
]
}
## Tests ##
var options = {
arrayMap: {
nicknames: "name"
}
};
js2xmlparser includes a set of tests to verify core functionality. You can run
the tests using gulp:
console.log(js2xmlparser("person", data, options));
```
gulp test
```
> <?xml version="1.0" encoding="UTF-8"?>
> <person>
> <firstName>John</firstName>
> <lastName>Smith</lastName>
> <nicknames>
> <name>Johnny</name>
> <name>Jon</name>
> <name>Jack</name>
> </nicknames>
> </person>
The `test` target builds the production variant of js2xmlparser before running
the tests. The `test-prod` target does the same thing, while the `test-dev`
target builds the development variant first instead.
This example wraps strings in CDATA tags instead of escaping invalid characters:
var js2xmlparser = require("js2xmlparser");
var data = {
"notes": {
"@": {
"type": "status"
},
"#": "John's profile is not complete."
}
};
var options = {
useCDATA: true
};
console.log(js2xmlparser("person", data, options));
> <?xml version="1.0" encoding="UTF-8"?>
> <person>
> <notes type="status"><![CDATA[John's profile is not complete.]]></notes>
> </person>
## Tests ##
js2xmlparser comes with a set of tests that evaluate and verify the package's core functionality. To run the tests:
* Install the test dependencies with `npm install`.
* Run the tests with `mocha`.
## License ##
j2xmlparser is licensed under the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0). Please see the
LICENSE.md file for more information.
js2xmlparser is licensed under the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).
Please see the LICENSE.md file for more information.
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