Skip to content
Browse files

Add parseFileSync and parseStringSync.

Having a synchronous version makes some operations in
client libraries easier.
  • Loading branch information...
1 parent 7b13aa9 commit d5a5153179d6e3db4db38f7675f003dc4b371147 @trotter committed Dec 11, 2011
Showing with 41 additions and 29 deletions.
  1. +3 −1 example.js
  2. +38 −28 lib/node-xml.js
View
4 example.js
@@ -50,6 +50,8 @@ parser.parseString(" cdata ]]>");
parser.parseString("</body");
parser.parseString("></html>");
-
+//example read synchronously
+parser.parseFileSync("sample.xml");
+sys.puts("All Done!");
View
66 lib/node-xml.js
@@ -787,6 +787,14 @@ SaxParser.CMNT = 9;
SaxParser.DTD_B = 10;
SaxParser.DTD_E = 11;
+SaxParser.prototype.parseFileSync = function(filename) {
+ var fs = require('fs');
+ var that = this;
+ fs.readFile(filename, function (err, data) {
+ that.parseStringSync(data);
+ });
+}
+
SaxParser.prototype.parseFile = function(filename) { //This function will only work in the node.js environment.
var fs = require('fs');
var that = this;
@@ -795,41 +803,43 @@ SaxParser.prototype.parseFile = function(filename) { //This function will only w
});
}
-
-SaxParser.prototype.parseString = function(strD) {
+SaxParser.prototype.parseStringSync = function(strD) {
sys = require('sys');
- var that = this;
- setTimeout(function(){
- var startnew = true;
- if(!that.m_parser){
- that.m_parser = new XMLP(strD);
- startnew = false;
- }
- else{
- that.m_parser.continueParsing(strD);
- startnew = true;
- }
+ var startnew = true;
+ if(!this.m_parser){
+ this.m_parser = new XMLP(strD);
+ startnew = false;
+ }
+ else{
+ this.m_parser.continueParsing(strD);
+ startnew = true;
+ }
- //if(that.m_hndDoc && that.m_hndDoc.setDocumentLocator) {
- // that.m_hndDoc.setDocumentLocator(that);
- //}
+ //if(that.m_hndDoc && that.m_hndDoc.setDocumentLocator) {
+ // that.m_hndDoc.setDocumentLocator(that);
+ //}
- that.m_bErr = false;
+ this.m_bErr = false;
- if(!that.m_bErr && !startnew) {
- that._fireEvent(SaxParser.DOC_B);
- }
- that._parseLoop();
- if(!that.m_bErr && !that.m_interrupted) {
- that._fireEvent(SaxParser.DOC_E);
- }
+ if(!this.m_bErr && !startnew) {
+ this._fireEvent(SaxParser.DOC_B);
+ }
+ this._parseLoop();
+ if(!this.m_bErr && !this.m_interrupted) {
+ this._fireEvent(SaxParser.DOC_E);
+ }
- that.m_xml = null;
- that.m_iP = 0;
- that.m_interrupted = false;
- }, 0);
+ this.m_xml = null;
+ this.m_iP = 0;
+ this.m_interrupted = false;
+}
+SaxParser.prototype.parseString = function(strD) {
+ var that = this;
+ setTimeout(function(){
+ that.parseStringSync(strD);
+ }, 0);
}
SaxParser.prototype.pause = function() {

0 comments on commit d5a5153

Please sign in to comment.
Something went wrong with that request. Please try again.