-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoXML_demo_2.js
More file actions
76 lines (66 loc) · 2.36 KB
/
toXML_demo_2.js
File metadata and controls
76 lines (66 loc) · 2.36 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
(function() {
// create a XMLObject constructor function, inheriting from Object
var XMLObject = (function(Parent) {
var Temp = function() {};
var Child = function() {};
Temp.prototype = Parent.prototype;
Child.prototype = new Temp();
Child.parent = Parent.prototype;
Child.prototype.constructor = Child;
return Child;
})(Object);
// inputs:
// options: a option hash
// options[root]: xml root tag, "root" if unspecified
//
// output:
// -1 if parsing failed
// a XML string if parsing successful
XMLObject.prototype.toXML = function(options) {
var obj_to_xml = function(obj) {
var result = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
result.push("<" + key + ">");
var val = obj[key];
if (val instanceof Function) {
return -1;
} else if (val != null && val instanceof Object) {
// since null is also an object, avoid passing it to recursion
var val_result = obj_to_xml(val);
if (val_result === -1) {
return -1;
}
result = result.concat(val_result);
} else {
result.push(val + "");
}
result.push("</" + key + ">");
}
}
return result;
};
var options = options || {};
var root = options["root"] || "root";
// uses an array of strings and joining only in the end to reduce memory usage
var result = [];
result.push("<" + root + ">");
var obj_result = obj_to_xml(this);
if (obj_result === -1) {
return -1;
}
result = result.concat(obj_result);
result.push("</" + root + ">");
return result.join("");
};
var input_1 = {
name: 'John',
addr: {
city: 'San Francisco',
state: 'CA'
}
};
console.log("usage 4: adding the function to child class");
// use XMLObject.prototype.toXML as if input_1 were a XMLObject
console.log(XMLObject.prototype.toXML.call(input_1));
})();