Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var util = require('util');
  2. var sax = require('sax');
  3. var TreeBuilder = require('./../treebuilder').TreeBuilder;
  4. function XMLParser(target) {
  5. this.parser = sax.parser(true);
  6. this.target = (target) ? target : new TreeBuilder();
  7. this.parser.onopentag = this._handleOpenTag.bind(this);
  8. this.parser.ontext = this._handleText.bind(this);
  9. this.parser.oncdata = this._handleCdata.bind(this);
  10. this.parser.ondoctype = this._handleDoctype.bind(this);
  11. this.parser.oncomment = this._handleComment.bind(this);
  12. this.parser.onclosetag = this._handleCloseTag.bind(this);
  13. this.parser.onerror = this._handleError.bind(this);
  14. }
  15. XMLParser.prototype._handleOpenTag = function(tag) {
  16. this.target.start(tag.name, tag.attributes);
  17. };
  18. XMLParser.prototype._handleText = function(text) {
  19. this.target.data(text);
  20. };
  21. XMLParser.prototype._handleCdata = function(text) {
  22. this.target.data(text);
  23. };
  24. XMLParser.prototype._handleDoctype = function(text) {
  25. };
  26. XMLParser.prototype._handleComment = function(comment) {
  27. };
  28. XMLParser.prototype._handleCloseTag = function(tag) {
  29. this.target.end(tag);
  30. };
  31. XMLParser.prototype._handleError = function(err) {
  32. throw err;
  33. };
  34. XMLParser.prototype.feed = function(chunk) {
  35. this.parser.write(chunk);
  36. };
  37. XMLParser.prototype.close = function() {
  38. this.parser.close();
  39. return this.target.close();
  40. };
  41. exports.XMLParser = XMLParser;