Browse files

Switching to create a file stream if a string is passed in.

Expecting an input stream and creating a file stream if the value is a string.
  • Loading branch information...
1 parent b8d121b commit bddcaecd64b0fe0c332cc1a34370e1d2ed4d2867 @Zoramite Zoramite committed Mar 28, 2012
Showing with 9 additions and 4 deletions.
  1. +9 −4 lib/xml2object.js
View
13 lib/xml2object.js
@@ -8,7 +8,7 @@ var fs = require('fs');
var sax = require('sax');
var util = require('util');
-var xml2object = module.exports = function(xmlFile, elements) {
+var xml2object = module.exports = function(source, elements) {
elements = elements || [];
this._hasStarted = false;
@@ -19,9 +19,14 @@ var xml2object = module.exports = function(xmlFile, elements) {
var inObjectName;
var ancestors = [];
- if(xmlFile) {
- this.fileStream = fs.createReadStream(xmlFile);
+ // Allowing for passing in a readable stream
+ this.inputStream = source;
+
+ // Allow for passing a filename
+ if(typeof this.inputStream === 'string') {
+ this.inputStream = fs.createReadStream(this.inputStream);
}
+
this.saxStream = sax.createStream(true);
this.saxStream.on("opentag", function (args) {
@@ -147,5 +152,5 @@ xml2object.prototype.start = function() {
this.emit('start');
// Start the streaming!
- this.fileStream.pipe(this.saxStream);
+ this.inputStream.pipe(this.saxStream);
}

0 comments on commit bddcaec

Please sign in to comment.