-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLTree.js
67 lines (66 loc) · 1.36 KB
/
XMLTree.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
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
var attribs = function() {
var name;
var value;
setter = function(name,value) {
this.name = name;
this.value = value;
};
return {
name : name,
value : value,
setter : setter
};
};
var xmlTree = function(root) {
var name =root;
var childs = new Array();
var attribs = new Array();
var construct = function(data) {
var that = this;
if(data instanceof xmlTree) {
that.childs.push(data);
}
else if(data instanceof attribs) {
that.attribs.push(data);
}
else {
console.log("not a valid data type for insertion into tree");
}
};
var traverse = function(root) {
var stack = new Stack();
var list = new Array();
stack.push(root);
while(stack.height>0) {
var now = stack.pop();
list.push(now);
for(var i=0;i<now.childs.length;i++) {
stack.push(now.childs[i]);
}
}
return list;
};
var searchByTag = function(root,node,attrib) {
var stack = new Stack();
var list = new Array();
stack.push(root);
while(stack.height>0) {
var now = stack.pop();
if(node.attrib == now.attrib) {
list.push(now);
}
for(var i=now.childs.length-1;i>=0;i--) {
stack.push(now.childs[i]);
}
}
return list;
};
return {
name: name,
childs : childs,
attribs : attribs,
construct : construct,
traverse : traverse,
searchByTag : searchByTag
};
};