Skip to content
Browse files

stripping namespaces

  • Loading branch information...
1 parent 719873f commit 045cbd6fdc8eda3f14e5bf16a9ce17da533fb411 @seanhess seanhess committed Sep 21, 2012
Showing with 50 additions and 18 deletions.
  1. +9 −1 README.md
  2. +8 −16 index.coffee
  3. +1 −1 package.json
  4. +32 −0 test.coffee
View
10 README.md
@@ -87,7 +87,7 @@ Every child node is put into the `$children` array
API Reference
-------------
- exports.parse = (nodeReadStream) ->
+ exports.parse = (nodeReadStream, [options]) ->
# returns a Parser
class Parser
@@ -103,4 +103,12 @@ API Reference
resume: ->
+Options
+----------
+
+ {
+ # removes all namespace information from the node names
+ stripNamespaces: false
+ }
+
View
24 index.coffee
@@ -1,27 +1,18 @@
expat = require 'node-expat'
events = require 'events'
-
-# simply parse a read stream as xml
-# give the system the base node,
-
-exports.parse = (readStream) ->
+exports.parse = (readStream, options = {}) ->
+ options.stripNamespaces ?= true
parser = new expat.Parser("UTF-8")
emitter = new events.EventEmitter()
- readStream.on 'data', (data) ->
- parser.parse data.toString()
+ readStream.on 'data', (data) -> parser.parse data.toString()
+ readStream.on 'end', -> process.nextTick -> emitter.emit 'end'
+ readStream.on 'error', (err) -> emitter.emit 'error', err
+ readStream.on 'close', -> emitter.emit 'close'
- readStream.on 'end', ->
- process.nextTick ->
- emitter.emit 'end'
- readStream.on 'error', (err) ->
- emitter.emit 'error', err
-
- readStream.on 'close', ->
- emitter.emit 'close'
# parse EVERYTHING inside of them.
@@ -37,6 +28,7 @@ exports.parse = (readStream) ->
emitter.emit 'error', err
parser.on 'startElement', (name, attrs) ->
+ if options.stripNamespaces then name = stripNamespace name
if name is nodeName or currentNode
currentNode = {$name: name, $:attrs, $parent: currentNode}
@@ -75,4 +67,4 @@ exports.parse = (readStream) ->
resume: -> readStream.resume()
}
-
+stripNamespace = (name) -> name.replace /^.*:/, ""
View
2 package.json
@@ -2,7 +2,7 @@
"author": "Sean Hess <sean@i.tv>",
"name": "xml-object-stream",
"description": "Low-memory streaming xml parser for node.js. Returns each node as an object. Uses node-expat",
- "version": "0.1.0",
+ "version": "0.2.0",
"repository": {
"type": "git",
"url": "git://github.com/idottv/xml-object-stream.git"
View
32 test.coffee
@@ -76,4 +76,36 @@ describe "xml streamer thing", ->
done()
+ describe 'namespaces', ->
+ it 'should strip namespaces by default', (done) ->
+ stream = streamData """
+ <root>
+ <me:item>one</me:item>
+ </root>
+ """
+
+ found = []
+ parser = parse stream
+ parser.each 'item', (item) ->
+ found.push item
+ assert.equal item.$text, "one"
+ parser.on 'end', ->
+ assert.equal found.length, 1
+ done()
+
+ it 'should preserve them if you turn it off', (done) ->
+ stream = streamData """
+ <root>
+ <me:item>one</me:item>
+ </root>
+ """
+
+ found = []
+ parser = parse stream, {stripNamespaces: false}
+ parser.each 'me:item', (item) ->
+ found.push item
+ assert.equal item.$text, "one"
+ parser.on 'end', ->
+ assert.equal found.length, 1
+ done()

0 comments on commit 045cbd6

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