Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 3 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
Commits on Oct 05, 2013
@lucsky Added example directory. 8661197
@lucsky API change.
Tag events don't really need the node name parameter  so we only pass the attributes.
01e8c64
@lucsky Fixed example after API change. bba3892
Showing with 81 additions and 23 deletions.
  1. +35 −0 examples/contacts.js
  2. +18 −0 examples/contacts.xml
  3. +1 −1 lib/exml.js
  4. +1 −1 package.json
  5. +1 −1 test/fixtures/simple.xml
  6. +25 −20 test/test.js
View
35 examples/contacts.js
@@ -0,0 +1,35 @@
+var fs = require('fs');
+var exml = require('../lib/exml');
+
+var parser = new exml.Parser();
+
+var name = null;
+var contacts = [];
+
+parser.on('address-book', function(attributes) {
+ name = attributes.name;
+
+ parser.on('contact', function(attributes) {
+ var contact = {};
+ contacts.push(contact);
+
+ parser.on('first-name', '$content', function(content) {
+ contact.firstName = content;
+ });
+ parser.on('last-name', '$content', function(content) {
+ contact.lastName = content;
+ });
+ parser.on('address', '$content', function(content) {
+ contact.address = content;
+ });
+ });
+});
+
+fs.readFile('contacts.xml', function(err, data) {
+ parser.end(data);
+ console.log('Address book: ' + name);
+ contacts.forEach(function(c) {
+ console.log('- ' + c.firstName + ' ' + c.lastName );
+ console.log(' ' + c.address);
+ })
+});
View
18 examples/contacts.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0"?>
+<address-book name="homies">
+ <contact>
+ <first-name>Tim</first-name>
+ <last-name>Cook</last-name>
+ <address>Cupertino</address>
+ </contact>
+ <contact>
+ <first-name>Steve</first-name>
+ <last-name>Ballmer</last-name>
+ <address>Redmond</address>
+ </contact>
+ <contact>
+ <first-name>Mark</first-name>
+ <last-name>Zuckerberg</last-name>
+ <address>Menlo Park</address>
+ </contact>
+</address-book>
View
2 lib/exml.js
@@ -37,7 +37,7 @@ Parser.prototype._handleOpenTag = function(node) {
this._frameStack.push(_newFrame());
if (handler) {
- handler(node.name, node.attributes);
+ handler(node.attributes);
}
};
View
2 package.json
@@ -1,6 +1,6 @@
{
"name": "exml",
- "version": "0.1.0",
+ "version": "0.2.0",
"description": "An event based XML parsing library using SAX under the hood for speed and efficiency.",
"keywords": ["xml", "events"],
"author": "Luc Heinrich <luc@honk-honk.com>",
View
2 test/fixtures/simple.xml
@@ -4,6 +4,6 @@
<node attr1="node2.attr1" attr2="node2.attr2" />
<node attr1="node3.attr1" attr2="node3.attr2" />
<node attr1="node4.attr1" attr2="node4.attr2">
- <subnode />
+ <subnode attr1="subnode.attr1" attr2="subnode.attr2" />
</node>
</root>
View
45 test/test.js
@@ -14,18 +14,17 @@ module.exports['node events'] = {
'local': function(test) {
var parser = this.parser;
- parser.on('root', function(name, attributes) {
- test.equal(name, 'root');
+ parser.on('root', function(attributes) {
test.equal(attributes.attr1, 'root.attr1');
test.equal(attributes.attr2, 'root.attr2');
var nodeIndex = 1;
- parser.on('node', function(name, attributes) {
- test.equal(name, 'node');
+ parser.on('node', function(attributes) {
test.equal(attributes.attr1, 'node' + nodeIndex + '.attr1');
test.equal(attributes.attr2, 'node' + nodeIndex + '.attr2');
nodeIndex++;
- parser.on('subnode', function(name) {
- test.equal(name, 'subnode');
+ parser.on('subnode', function(attributes) {
+ test.equal(attributes.attr1, 'subnode.attr1');
+ test.equal(attributes.attr2, 'subnode.attr2');
});
});
});
@@ -35,22 +34,21 @@ module.exports['node events'] = {
},
'stacked': function(test) {
- this.parser.on('root', function(name, attributes) {
- test.equal(name, 'root');
+ this.parser.on('root', function(attributes) {
test.equal(attributes.attr1, 'root.attr1');
test.equal(attributes.attr2, 'root.attr2');
});
var nodeIndex = 1;
- this.parser.on('root', 'node', function(name, attributes) {
- test.equal(name, 'node');
+ this.parser.on('root', 'node', function(attributes) {
test.equal(attributes.attr1, 'node' + nodeIndex + '.attr1');
test.equal(attributes.attr2, 'node' + nodeIndex + '.attr2');
nodeIndex++;
});
- this.parser.on('root', 'node', 'subnode', function(name) {
- test.equal(name, 'subnode');
+ this.parser.on('root', 'node', 'subnode', function(attributes) {
+ test.equal(attributes.attr1, 'subnode.attr1');
+ test.equal(attributes.attr2, 'subnode.attr2');
});
this.parser.end(SIMPLE_XML);
@@ -59,10 +57,14 @@ module.exports['node events'] = {
'partially stacked 1': function(test) {
var parser = this.parser;
- parser.on('root', 'node', function(name, attributes) {
- test.equal(name, 'node');
- parser.on('subnode', function(name) {
- test.equal(name, 'subnode');
+ var nodeIndex = 1;
+ parser.on('root', 'node', function(attributes) {
+ test.equal(attributes.attr1, 'node' + nodeIndex + '.attr1');
+ test.equal(attributes.attr2, 'node' + nodeIndex + '.attr2');
+ nodeIndex++;
+ parser.on('subnode', function(attributes) {
+ test.equal(attributes.attr1, 'subnode.attr1');
+ test.equal(attributes.attr2, 'subnode.attr2');
});
});
@@ -72,10 +74,13 @@ module.exports['node events'] = {
'partially stacked 2': function(test) {
var parser = this.parser;
- parser.on('root', function(name, attributes) {
- test.equal(name, 'root');
- parser.on('node', 'subnode', function(name) {
- test.equal(name, 'subnode');
+ parser.on('root', function(attributes) {
+ test.equal(attributes.attr1, 'root.attr1');
+ test.equal(attributes.attr2, 'root.attr2');
+ var nodeIndex = 1;
+ parser.on('node', 'subnode', function(attributes) {
+ test.equal(attributes.attr1, 'subnode.attr1');
+ test.equal(attributes.attr2, 'subnode.attr2');
});
});

No commit comments for this range

Something went wrong with that request. Please try again.