Skip to content
This repository
Browse code

Working on changing the argument order.

  • Loading branch information...
commit 513b1c25b77f4f228f3ebb5d4661cf251ebfbf47 1 parent bddcaec
Randy Merrill authored March 28, 2012
17  lib/xml2object.js
@@ -6,9 +6,10 @@
6 6
 var emitter = require('events').EventEmitter;
7 7
 var fs = require('fs');
8 8
 var sax = require('sax');
  9
+var stream = require('stream');
9 10
 var util = require('util');
10 11
 
11  
-var xml2object = module.exports = function(source, elements) {
  12
+var xml2object = module.exports = function(elements, source) {
12 13
 	elements = elements || [];
13 14
 	
14 15
 	this._hasStarted = false;
@@ -19,11 +20,11 @@ var xml2object = module.exports = function(source, elements) {
19 20
 	var inObjectName;
20 21
 	var ancestors = [];
21 22
 
22  
-	// Allowing for passing in a readable stream
23  
-	this.inputStream = source;
24  
-
25  
-	// Allow for passing a filename
26  
-	if(typeof this.inputStream === 'string') {
  23
+	if(source instanceof Stream) {
  24
+		// Allowing for passing in a readable stream
  25
+		this.inputStream = source;
  26
+	if(typeof source === 'string') {
  27
+		// Allow for passing a filename
27 28
 		this.inputStream = fs.createReadStream(this.inputStream);
28 29
 	}
29 30
 
@@ -142,8 +143,8 @@ var xml2object = module.exports = function(source, elements) {
142 143
 util.inherits(xml2object, emitter);
143 144
 
144 145
 xml2object.prototype.start = function() {
145  
-	// Can only start once
146  
-	if(this._hasStarted) {
  146
+	// Can only start once and must have a stream
  147
+	if(this._hasStarted || !this.inputStream) {
147 148
 		return;
148 149
 	}
149 150
 
59  readme.md
Source Rendered
@@ -10,12 +10,12 @@ Converts xml elements into JavaScript objects.
10 10
 
11 11
 ## Usage
12 12
 
13  
-### From a file
  13
+### From A File Name
14 14
 
15 15
     var xml2object = require('xml2object');
16 16
     
17 17
     // Create a new xml parser with an array of xml elements to look for
18  
-    var parser = new xml2object('myAnimals.xml', [ 'animal' ]);
  18
+    var parser = new xml2object([ 'animal' ], 'myAnimals.xml');
19 19
     
20 20
     // Bind to the object event to work with the objects found in the XML file
21 21
     parser.on('object', function(name, obj) {
@@ -31,13 +31,37 @@ Converts xml elements into JavaScript objects.
31 31
     // Start parsing the XML
32 32
     parser.start();
33 33
 
34  
-### From a stream
  34
+### From An Input Stream
  35
+
  36
+    var xml2object = require('xml2object');
  37
+    var source = fs.createReadStream('/path/to/file.xml');
  38
+    
  39
+    // Create a new xml parser with an array of xml elements to look for
  40
+    var parser = new xml2object([ 'animal' ], source);
  41
+    
  42
+    // Bind to the object event to work with the objects found in the XML file
  43
+    parser.on('object', function(name, obj) {
  44
+        console.log('Found an object: %s', name);
  45
+        console.log(obj);
  46
+    });
  47
+
  48
+    // Bind to the file end event to tell when the file is done being streamed
  49
+    parser.on('end', function(name, obj) {
  50
+        console.log('Finished parsing xml!');
  51
+    });
  52
+
  53
+    // Start parsing the input stream
  54
+    parser.start();
  55
+
  56
+### Piped From An Input Stream
  57
+
  58
+_Note:_ The following example uses the [`request`][1] package to simplify the http request.
35 59
 
36 60
     var xml2object = require('xml2object');
37 61
     var request = require('request');
38 62
     
39 63
     // Create a new xml parser with an array of xml elements to look for
40  
-    var parser = new xml2object(null, [ 'animal' ]);
  64
+    var parser = new xml2object([ 'animal' ]);
41 65
     
42 66
     // Bind to the object event to work with the objects found in the XML file
43 67
     parser.on('object', function(name, obj) {
@@ -50,24 +74,31 @@ Converts xml elements into JavaScript objects.
50 74
         console.log('Finished parsing xml!');
51 75
     });
52 76
 
  77
+    // Pipe a request into the parser
53 78
     request.get('http://www.example.com/test.xml').pipe(parser.saxStream);
54 79
 
55 80
 ## Module
56 81
 
57  
-### xml2object(xmlFile, elements)
  82
+### xml2object(elements, source)
58 83
 
59 84
 Constructor for creating an instance of the xml parser.
60  
-The xmlFile argument is optional. If not specified you need to pipe your own Stream into the parser.saxStream.
  85
+
  86
+The source argument is optional and can be a path to an xml file or an input stream.
  87
+
  88
+If no source is specified you can set a readable Stream to `.inputStream` or pipe a Stream into the `.saxStream`.
61 89
 
62 90
     var xml2object = require('xml2object');
63 91
     
64 92
     // Parse the myAnimals.xml file looking for <animal> elements
65  
-    var parser = new xml2object('myAnimals.xml', [ 'animal' ]);
  93
+    var parser = new xml2object([ 'animal' ], 'myAnimals.xml');
  94
+
  95
+### .inputSource
  96
+
  97
+The input Stream used as a source for parsing.
66 98
 
67 99
 ### .saxStream
68 100
 
69  
-The underlying saxStream. When not using the xmlFile argument in the constructor this stream can be used as
70  
-a destination for pipe().
  101
+The underlying sax Stream. Data can be piped directly to the sax Stream using the `pipe()`.
71 102
 
72 103
 ### .start()
73 104
 
@@ -76,8 +107,6 @@ Triggers the xml file to start streaming to the parser. Call this method after y
76 107
     // Start parsing the XML
77 108
     parser.start();
78 109
 
79  
-
80  
-
81 110
 ### Event: 'object'
82 111
 
83 112
     function(name, obj) { ... }
@@ -90,6 +119,14 @@ Triggered when an object has been parsed from the XML file with the name of the
90 119
 
91 120
 Marks the end of the input file when it has been completely streamed through the parser.
92 121
 
  122
+### Event: 'start'
  123
+
  124
+    function() { ... }
  125
+
  126
+Marks the start of reading from the source. This event will not fire if using the `.saxParser.pipe()` method.
  127
+
93 128
 ## Other Notes
94 129
 
95 130
 Elements being parsed cannot currently be nested. For example. if you have `root > bikes > bike > wheel` as a heirarchy and have done a `xml2object('transportation.xml', [ 'bike', 'wheel' ])` the bike objects will be returned, but the wheel elements inside the bike element will not be parsed separately.
  131
+
  132
+  [1]: https://github.com/mikeal/request

0 notes on commit 513b1c2

Please sign in to comment.
Something went wrong with that request. Please try again.