/
reader.js
35 lines (34 loc) · 855 Bytes
/
reader.js
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
var fs = require("fs");
var dom = require('xmldom').DOMParser;
var xpath = require('xpath');
function find(xp){
var found = xpath.select(xp, this);
return found;
}
function value(xp){
var found = xpath.select1(xp, this);
if(found) {
return found.value || innerText(found);
}
}
function innerText(node) {
var out = "";
for (var child = node.firstChild; !!child; child=child.nextSibling) {
if (child.nodeType == 3)//#text
out += child.nodeValue;
else if (child.nodeType == 1) //element
out += innerText(child);
}
return out;
};
exports = module.exports = {
data: "./core/common",
open: function(dir, name){
var fullpath = exports.data + "/" + dir + "/" + name + ".xml";
var txt = fs.readFileSync(fullpath, "utf8");
var xmlDoc = new dom().parseFromString(txt);
xmlDoc.find = find;
xmlDoc.value = value;
return xmlDoc;
}
}