-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathxml.js
More file actions
85 lines (69 loc) · 1.92 KB
/
xml.js
File metadata and controls
85 lines (69 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var Parser = require('node-expat').Parser;
var has = {}.hasOwnProperty;
function startElement(name, attr) {
//console.log('START', name, attr);
var node = {name: name, attr: attr, ch: [], parent: this.currentNode, byName: {} };
this.currentNode.ch.push(node);
this.currentNode = node;
}
function endElement(name) {
//console.log('END', name);
if (has.call(this.listeners, name)) {
this.listeners[name].call(this, this.currentNode);
}
var parent = this.currentNode.parent;
if ( !has.call(parent.byName, name) )
parent.byName[name] = [];
parent.byName[name].push(this.currentNode);
this.currentNode = parent;
}
function text(txt) {
//console.log('TEXT', text );
var txt = this.preprocessText.call(this, txt);
if (txt === "")
return;
this.currentNode.ch.push({
name: '#text', attr: [], ch: txt, parent: this.currentNode });
}
function bind(func, _this) {
return function() {
return func.apply(_this, arguments);
};
}
function nospace(txt) {
var e = 0;
while (e < txt.length) {
switch(txt.charAt(e)) {
case '\t':
case ' ':
case '\n':
return "";
default:
e++;
}
}
return txt;
}
function error(e) {
this.error = e;
}
function XMLParser() {
this.currentNode = {name: '#document', attr: [], ch: [], parent: null, byName: {} } ;
this.parser = new Parser();
this.listeners = {};
this.preprocessText = nospace;
this.parser.on('startElement', bind(startElement, this) );
this.parser.on('endElement', bind(endElement, this) );
this.parser.on('text', bind(text, this) );
this.error = null;
this.parser.on('error', bind(error, this) ) ;
}
XMLParser.prototype.parseString = function(str) {
return this.parser.write(str);
};
XMLParser.prototype.parseFile = function(fileName) {
var fs = require('fs');
return this.parseString(fs.readFileSync(fileName, 'utf-8'));
};
var e = module.exports;
e.XMLParser = XMLParser;