Last active
August 29, 2015 14:06
-
-
Save paulodiovani/27bef9e0ad4bdd28745d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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