Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

stripping namespaces

  • Loading branch information...
commit 045cbd6fdc8eda3f14e5bf16a9ce17da533fb411 1 parent 719873f
Sean Hess seanhess authored

Showing 4 changed files with 50 additions and 18 deletions. Show diff stats Hide diff stats

  1. +9 1 README.md
  2. +8 16 index.coffee
  3. +1 1  package.json
  4. +32 0 test.coffee
10 README.md
Source Rendered
@@ -87,7 +87,7 @@ Every child node is put into the `$children` array
87 87 API Reference
88 88 -------------
89 89
90   - exports.parse = (nodeReadStream) ->
  90 + exports.parse = (nodeReadStream, [options]) ->
91 91 # returns a Parser
92 92
93 93 class Parser
@@ -103,4 +103,12 @@ API Reference
103 103 resume: ->
104 104
105 105
  106 +Options
  107 +----------
  108 +
  109 + {
  110 + # removes all namespace information from the node names
  111 + stripNamespaces: false
  112 + }
  113 +
106 114
24 index.coffee
... ... @@ -1,27 +1,18 @@
1 1 expat = require 'node-expat'
2 2 events = require 'events'
3 3
4   -
5   -# simply parse a read stream as xml
6   -# give the system the base node,
7   -
8   -exports.parse = (readStream) ->
  4 +exports.parse = (readStream, options = {}) ->
  5 + options.stripNamespaces ?= true
9 6
10 7 parser = new expat.Parser("UTF-8")
11 8 emitter = new events.EventEmitter()
12 9
13   - readStream.on 'data', (data) ->
14   - parser.parse data.toString()
  10 + readStream.on 'data', (data) -> parser.parse data.toString()
  11 + readStream.on 'end', -> process.nextTick -> emitter.emit 'end'
  12 + readStream.on 'error', (err) -> emitter.emit 'error', err
  13 + readStream.on 'close', -> emitter.emit 'close'
15 14
16   - readStream.on 'end', ->
17   - process.nextTick ->
18   - emitter.emit 'end'
19 15
20   - readStream.on 'error', (err) ->
21   - emitter.emit 'error', err
22   -
23   - readStream.on 'close', ->
24   - emitter.emit 'close'
25 16
26 17
27 18 # parse EVERYTHING inside of them.
@@ -37,6 +28,7 @@ exports.parse = (readStream) ->
37 28 emitter.emit 'error', err
38 29
39 30 parser.on 'startElement', (name, attrs) ->
  31 + if options.stripNamespaces then name = stripNamespace name
40 32 if name is nodeName or currentNode
41 33 currentNode = {$name: name, $:attrs, $parent: currentNode}
42 34
@@ -75,4 +67,4 @@ exports.parse = (readStream) ->
75 67 resume: -> readStream.resume()
76 68 }
77 69
78   -
  70 +stripNamespace = (name) -> name.replace /^.*:/, ""
2  package.json
@@ -2,7 +2,7 @@
2 2 "author": "Sean Hess <sean@i.tv>",
3 3 "name": "xml-object-stream",
4 4 "description": "Low-memory streaming xml parser for node.js. Returns each node as an object. Uses node-expat",
5   - "version": "0.1.0",
  5 + "version": "0.2.0",
6 6 "repository": {
7 7 "type": "git",
8 8 "url": "git://github.com/idottv/xml-object-stream.git"
32 test.coffee
@@ -76,4 +76,36 @@ describe "xml streamer thing", ->
76 76 done()
77 77
78 78
  79 + describe 'namespaces', ->
  80 + it 'should strip namespaces by default', (done) ->
  81 + stream = streamData """
  82 + <root>
  83 + <me:item>one</me:item>
  84 + </root>
  85 + """
  86 +
  87 + found = []
  88 + parser = parse stream
  89 + parser.each 'item', (item) ->
  90 + found.push item
  91 + assert.equal item.$text, "one"
  92 + parser.on 'end', ->
  93 + assert.equal found.length, 1
  94 + done()
  95 +
  96 + it 'should preserve them if you turn it off', (done) ->
  97 + stream = streamData """
  98 + <root>
  99 + <me:item>one</me:item>
  100 + </root>
  101 + """
  102 +
  103 + found = []
  104 + parser = parse stream, {stripNamespaces: false}
  105 + parser.each 'me:item', (item) ->
  106 + found.push item
  107 + assert.equal item.$text, "one"
  108 + parser.on 'end', ->
  109 + assert.equal found.length, 1
  110 + done()
79 111

0 comments on commit 045cbd6

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