Skip to content

Instantly share code, notes, and snippets.

@paulodiovani
Last active August 29, 2015 14:06
Show Gist options
  • Save paulodiovani/27bef9e0ad4bdd28745d to your computer and use it in GitHub Desktop.
Save paulodiovani/27bef9e0ad4bdd28745d to your computer and use it in GitHub Desktop.
http = require 'http'
through = require 'through'
XmlStream = require 'xml-stream'
http.get 'http://example.com/file.xml', (res) ->
res.setEncoding 'utf-8'
res.on 'error', console.error
# create a stream to clean non-ascii characters, because
# XmlStream fails to parse XMLs with some chars (pasted from MS Word, mostly)
tr = through (buffer) ->
data = buffer.toString().replace(/[^\x00-\xFF]/g, '')
@emit 'data', data
# pipe for new stream
res.pipe tr
# process xml
xml = new XmlStream(tr)
xml.collect 'node subnode' #collect elements to array
xml.on 'updateElement: node', (node) ->
# output for testing
console.log JSON.stringify node, null, ' '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment