Comparing version 0.2.8 to 0.4.0
// Generated by CoffeeScript 1.6.3 | ||
(function() { | ||
var events, isEmpty, sax, | ||
var bom, builder, events, isEmpty, sax, | ||
__hasProp = {}.hasOwnProperty, | ||
@@ -12,2 +12,6 @@ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, | ||
builder = require('xmlbuilder'); | ||
bom = require('./bom'); | ||
isEmpty = function(thing) { | ||
@@ -54,3 +58,15 @@ return typeof thing === "object" && (thing != null) && Object.keys(thing).length === 0; | ||
async: false, | ||
strict: true | ||
strict: true, | ||
rootName: 'root', | ||
xmldec: { | ||
'version': '1.0', | ||
'encoding': 'UTF-8', | ||
'standalone': true | ||
}, | ||
doctype: null, | ||
renderOpts: { | ||
'pretty': true, | ||
'indent': ' ', | ||
'newline': '\n' | ||
} | ||
} | ||
@@ -70,2 +86,73 @@ }; | ||
exports.Builder = (function() { | ||
function Builder(opts) { | ||
var key, value, _ref; | ||
this.options = {}; | ||
_ref = exports.defaults["0.2"]; | ||
for (key in _ref) { | ||
if (!__hasProp.call(_ref, key)) continue; | ||
value = _ref[key]; | ||
this.options[key] = value; | ||
} | ||
for (key in opts) { | ||
if (!__hasProp.call(opts, key)) continue; | ||
value = opts[key]; | ||
this.options[key] = value; | ||
} | ||
} | ||
Builder.prototype.buildObject = function(rootObj) { | ||
var attrkey, charkey, render, rootElement, rootName; | ||
attrkey = this.options.attrkey; | ||
charkey = this.options.charkey; | ||
if ((Object.keys(rootObj).length === 1) && (this.options.rootName === exports.defaults['0.2'].rootName)) { | ||
rootName = Object.keys(rootObj)[0]; | ||
rootObj = rootObj[rootName]; | ||
} else { | ||
rootName = this.options.rootName; | ||
} | ||
render = function(element, obj) { | ||
var attr, child, entry, index, key, value, _ref, _ref1; | ||
if (typeof obj !== 'object') { | ||
element.txt(obj); | ||
} else { | ||
for (key in obj) { | ||
if (!__hasProp.call(obj, key)) continue; | ||
child = obj[key]; | ||
if (key === attrkey) { | ||
if (typeof child === "object") { | ||
for (attr in child) { | ||
value = child[attr]; | ||
element = element.att(attr, value); | ||
} | ||
} | ||
} else if (key === charkey) { | ||
element = element.txt(child); | ||
} else if (typeof child === 'object' && ((child != null ? child.constructor : void 0) != null) && ((child != null ? (_ref = child.constructor) != null ? _ref.name : void 0 : void 0) != null) && (child != null ? (_ref1 = child.constructor) != null ? _ref1.name : void 0 : void 0) === 'Array') { | ||
for (index in child) { | ||
if (!__hasProp.call(child, index)) continue; | ||
entry = child[index]; | ||
if (typeof entry === 'string') { | ||
element = element.ele(key, entry).up(); | ||
} else { | ||
element = arguments.callee(element.ele(key), entry).up(); | ||
} | ||
} | ||
} else if (typeof child === "object") { | ||
element = arguments.callee(element.ele(key), child).up(); | ||
} else { | ||
element = element.ele(key, child.toString()).up(); | ||
} | ||
} | ||
} | ||
return element; | ||
}; | ||
rootElement = builder.create(rootName, this.options.xmldec, this.options.doctype); | ||
return render(rootElement, rootObj).end(this.options.renderOpts); | ||
}; | ||
return Builder; | ||
})(); | ||
exports.Parser = (function(_super) { | ||
@@ -77,2 +164,3 @@ __extends(Parser, _super); | ||
this.reset = __bind(this.reset, this); | ||
this.assignOrPush = __bind(this.assignOrPush, this); | ||
var key, value, _ref; | ||
@@ -100,2 +188,17 @@ if (!(this instanceof exports.Parser)) { | ||
Parser.prototype.assignOrPush = function(obj, key, newValue) { | ||
if (!(key in obj)) { | ||
if (!this.options.explicitArray) { | ||
return obj[key] = newValue; | ||
} else { | ||
return obj[key] = [newValue]; | ||
} | ||
} else { | ||
if (!(obj[key] instanceof Array)) { | ||
obj[key] = [obj[key]]; | ||
} | ||
return obj[key].push(newValue); | ||
} | ||
}; | ||
Parser.prototype.reset = function() { | ||
@@ -123,3 +226,3 @@ var attrkey, charkey, err, ontext, stack, | ||
this.saxParser.onopentag = function(node) { | ||
var key, obj, _ref; | ||
var key, newValue, obj, _ref; | ||
obj = {}; | ||
@@ -134,6 +237,7 @@ obj[charkey] = ""; | ||
} | ||
newValue = node.attributes[key]; | ||
if (_this.options.mergeAttrs) { | ||
obj[key] = node.attributes[key]; | ||
_this.assignOrPush(obj, key, newValue); | ||
} else { | ||
obj[attrkey][key] = node.attributes[key]; | ||
obj[attrkey][key] = newValue; | ||
} | ||
@@ -209,18 +313,3 @@ } | ||
if (stack.length > 0) { | ||
if (!_this.options.explicitArray) { | ||
if (!(nodeName in s)) { | ||
return s[nodeName] = obj; | ||
} else if (s[nodeName] instanceof Array) { | ||
return s[nodeName].push(obj); | ||
} else { | ||
old = s[nodeName]; | ||
s[nodeName] = [old]; | ||
return s[nodeName].push(obj); | ||
} | ||
} else { | ||
if (!(s[nodeName] instanceof Array)) { | ||
s[nodeName] = []; | ||
} | ||
return s[nodeName].push(obj); | ||
} | ||
return _this.assignOrPush(s, nodeName, obj); | ||
} else { | ||
@@ -281,3 +370,3 @@ if (_this.options.explicitRoot) { | ||
} | ||
return this.saxParser.write(str.toString()); | ||
return this.saxParser.write(bom.stripBOM(str.toString())); | ||
}; | ||
@@ -284,0 +373,0 @@ |
@@ -6,3 +6,3 @@ { | ||
"homepage" : "https://github.com/Leonidas-from-XIV/node-xml2js", | ||
"version" : "0.2.8", | ||
"version" : "0.4.0", | ||
"author" : "Marek Kubica <marek@xivilization.net> (http://xivilization.net)", | ||
@@ -27,3 +27,6 @@ "contributors" : [ | ||
"Chris Tavares <ctavares@microsoft.com> (https://github.com/christav)", | ||
"Frank Xu <yyfrankyy@gmail.com> (http://f2e.us/)" | ||
"Frank Xu <yyfrankyy@gmail.com> (http://f2e.us/)", | ||
"Guido D'Albore <guido@bitstorm.it> (http://www.bitstorm.it/)", | ||
"Jack Senechal (http://jacksenechal.com/)", | ||
"Matthias Hölzl <tc@xantira.com> (https://github.com/hoelzl)" | ||
], | ||
@@ -42,3 +45,4 @@ "main" : "./lib/xml2js", | ||
"dependencies" : { | ||
"sax" : "0.5.x" | ||
"sax" : "0.5.x", | ||
"xmlbuilder" : ">=0.4.2" | ||
}, | ||
@@ -48,4 +52,11 @@ "devDependencies" : { | ||
"zap" : ">=0.2.5", | ||
"docco" : ">=0.6.2" | ||
} | ||
"docco" : ">=0.6.2", | ||
"diff" : ">=1.0.7" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://raw.github.com/Leonidas-from-XIV/node-xml2js/master/LICENSE" | ||
} | ||
] | ||
} |
@@ -11,4 +11,5 @@ node-xml2js | ||
Simple XML to JavaScript object converter. Uses | ||
[sax-js](https://github.com/isaacs/sax-js/). | ||
Simple XML to JavaScript object converter. It supports bi-directional conversion. | ||
Uses [sax-js](https://github.com/isaacs/sax-js/) and | ||
[xmlbuilder-js](https://github.com/oozcitak/xmlbuilder-js/). | ||
@@ -48,3 +49,3 @@ Note: If you're looking for a full DOM parser, you probably want | ||
```coffeescript | ||
parseString = require('xml2js').parseString | ||
{parseString} = require 'xml2js' | ||
xml = "<root>Hello xml2js!</root>" | ||
@@ -67,3 +68,3 @@ parseString xml, (err, result) -> | ||
That's right, if you have been using xml-simple or a home-grown | ||
wrapper, this is was added in 0.1.11 just for you: | ||
wrapper, this was added in 0.1.11 just for you: | ||
@@ -159,6 +160,25 @@ ```javascript | ||
XML builder usage | ||
----------------- | ||
Since 0.4.0, objects can be also be used to build XML: | ||
```javascript | ||
var fs = require('fs'), | ||
xml2js = require('xml2js'); | ||
var obj = {name: "Super", Surname: "Man", age: 23}; | ||
var builder = new xml2js.Builder(); | ||
var xml = builder.buildObject(obj); | ||
``` | ||
At the moment, a one to one bi-directional conversion is guaranteed only for | ||
default configuration, except for `attrkey`, `charkey` and `explicitArray` options | ||
you can redefine to your taste. Writing CDATA is not currently supported. | ||
Options | ||
======= | ||
Apart from the default settings, there is a number of options that can be | ||
Apart from the default settings, there are a number of options that can be | ||
specified for the parser. Options are specified by ``new Parser({optionName: | ||
@@ -208,2 +228,21 @@ value})``. Possible options are: | ||
Options for the `Builder` class | ||
------------------------------- | ||
* `rootName` (default `root`): root element name to be used in case | ||
`explicitiRoot` is `false` or to override the root element name. | ||
* `renderOpts` (default `{ 'pretty': true, 'indent': ' ', 'newline': '\n' }`): | ||
Rendering options for xmlbuilder-js. | ||
* pretty: prettify generated XML | ||
* indent: whitespace for indentation (only when pretty) | ||
* newline: newline char (only when pretty) | ||
* `xmldec` (default `{ 'version': '1.0', 'encoding': 'UTF-8', 'standalone': true }`: | ||
XML declaration attributes. | ||
* `xmldec.version` A version number string, e.g. 1.0 | ||
* `xmldec.encoding` Encoding declaration, e.g. UTF-8 | ||
* `xmldec.standalone` standalone document declaration: true or false | ||
* `doctype` (default `null`): optional DTD. Eg. `{'ext': 'hello.dtd'}` | ||
renderOpts, xmldec and doctype pass through to [xmlbuilder-js](https://github.com/oozcitak/xmlbuilder-js) | ||
Updating to new version | ||
@@ -243,2 +282,3 @@ ======================= | ||
[![Build Status](https://secure.travis-ci.org/Leonidas-from-XIV/node-xml2js.png?branch=master)](https://travis-ci.org/Leonidas-from-XIV/node-xml2js) | ||
[![Dependency Status](https://david-dm.org/Leonidas-from-XIV/node-xml2js.png)](https://david-dm.org/Leonidas-from-XIV/node-xml2js) | ||
@@ -245,0 +285,0 @@ The development requirements are handled by npm, you just need to install them. |
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
97057
12
377
289
2
4
+ Addedxmlbuilder@>=0.4.2
+ Addedxmlbuilder@15.1.1(transitive)