Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 75fcef1
Showing
7 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{spawn} = require 'child_process' | ||
fs = require 'fs' | ||
|
||
task 'build', 'coffee -> js', -> | ||
files = (file for file in fs.readdirSync '.' when file.match(/\.coffee$/)) | ||
proc = spawn 'coffee', ['-c'].concat files | ||
proc.stderr.on 'data', (buffer) -> console.log buffer.toString() | ||
proc.on 'exit', (status) -> process.exit(1) if status != 0 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
This module converts streams of XML to streams of JS objects. Unlike other | ||
SAX modules, it doesn't give you every tag and text node seperately, but lets you | ||
specify tag names you want to filter for. When this module encounters a tag with | ||
that name, its content will be collected until the closing tag was reached, and | ||
then you will receive the tag with all its attributes and children in one | ||
object. | ||
|
||
Compiling | ||
========= | ||
To compile the `.coffee` files to Javascript, use `cake build`. You will need | ||
[Coffeescript](http://coffeescript.org/) for that. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
sax = require 'sax' | ||
|
||
last = (array) -> array[array.length-1] | ||
|
||
exports.createParser = (cbError, cbFinished, wantedNodes, strict) -> | ||
interestingStack = [] | ||
element = null | ||
parser = sax.parser strict | ||
parser.onerror = cbError | ||
parser.onend = cbFinished | ||
parser.onopentag = ({name, attributes}) -> | ||
console.log " opening #{name}" | ||
if wantedNodes[name]? or element? | ||
parent = element | ||
element = {parent, name, attributes, children: []} | ||
parent?.children.push element | ||
if wantedNodes[name]? | ||
interestingStack.push name | ||
parser.onclosetag = (name) -> | ||
if name is last interestingStack | ||
interestingStack.pop() | ||
wantedNodes[name] element | ||
element = element?.parent | ||
parser.ontext = (text) -> | ||
element?.children.push text | ||
parser |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
(function() { | ||
var last, sax; | ||
sax = require('sax'); | ||
last = function(array) { | ||
return array[array.length - 1]; | ||
}; | ||
exports.createParser = function(cbError, cbFinished, wantedNodes, strict) { | ||
var element, interestingStack, parser; | ||
interestingStack = []; | ||
element = null; | ||
parser = sax.parser(strict); | ||
parser.onerror = cbError; | ||
parser.onend = cbFinished; | ||
parser.onopentag = function(_arg) { | ||
var attributes, name, parent; | ||
name = _arg.name, attributes = _arg.attributes; | ||
console.log(" opening " + name); | ||
if ((wantedNodes[name] != null) || (element != null)) { | ||
parent = element; | ||
element = { | ||
parent: parent, | ||
name: name, | ||
attributes: attributes, | ||
children: [] | ||
}; | ||
if (parent != null) { | ||
parent.children.push(element); | ||
} | ||
} | ||
if (wantedNodes[name] != null) { | ||
return interestingStack.push(name); | ||
} | ||
}; | ||
parser.onclosetag = function(name) { | ||
if (name === last(interestingStack)) { | ||
interestingStack.pop(); | ||
wantedNodes[name](element); | ||
} | ||
return element = element != null ? element.parent : void 0; | ||
}; | ||
parser.ontext = function(text) { | ||
return element != null ? element.children.push(text) : void 0; | ||
}; | ||
return parser; | ||
}; | ||
}).call(this); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ "name": "halfstreamxml" | ||
, "version": "0.1.0" | ||
, "description": "converts a stream of XML to a stream of objects" | ||
, "keywords": | ||
[ "XML" | ||
, "stream" | ||
, "SAX" | ||
] | ||
, "homepage": "http://github.com/thejh/node-halfstreamxml" | ||
, "author": "Jann Horn" | ||
, "main": "FIXME" | ||
, "repository": | ||
{ "type": "git" | ||
, "url": "http://github.com/thejh/node-halfstreamxml.git" | ||
} | ||
, "dependencies": | ||
{ "sax": "0.1.2" | ||
} | ||
, "files": | ||
{ "package.json" | ||
, "index.js" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
halfstreamxml = require './index' | ||
onerror = (e) -> | ||
console.error "ERROR: #{e}" | ||
onfinish = -> | ||
console.log "DONE" | ||
wantednodes = | ||
PERSON: (person) -> | ||
console.log "received person: #{JSON.stringify person, ['name', 'attributes', 'age', 'children']}" | ||
parser = halfstreamxml.createParser onerror, onfinish, wantednodes, false | ||
parser.write('<xml><person age="15"><name>Barfoo</name></person><person age="17"><name>Foo Bar</name></person></xml>').close() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
(function() { | ||
var halfstreamxml, onerror, onfinish, parser, wantednodes; | ||
halfstreamxml = require('./index'); | ||
onerror = function(e) { | ||
return console.error("ERROR: " + e); | ||
}; | ||
onfinish = function() { | ||
return console.log("DONE"); | ||
}; | ||
wantednodes = { | ||
PERSON: function(person) { | ||
return console.log("received person: " + (JSON.stringify(person, ['name', 'attributes', 'age', 'children']))); | ||
} | ||
}; | ||
parser = halfstreamxml.createParser(onerror, onfinish, wantednodes, false); | ||
parser.write('<xml><person age="15"><name>Barfoo</name></person><person age="17"><name>Foo Bar</name></person></xml>').close(); | ||
}).call(this); |