-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraw_settings.js
78 lines (70 loc) · 2.17 KB
/
raw_settings.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
68
69
70
71
72
73
74
75
76
77
78
// Related Question: http://stackoverflow.com/questions/31845720/node-js-xml-to-json-objects
//
// In Node, I'm trying to migrate an XML structure into JSON, which would allow me to
// access specific nodes without arrays, but guess issue is common for all ECMA related languages
//
// Basically I'm looking for a way of doing this:
//
// var siteSettings = configuration.settings.siteSettings
// var site1 = siteSettings[0];
// var site1Name = site1.name;
// where the XML is this
//---------------------------
// <configuration>
// <settings>
// <siteSettings>
// <siteSetting name="site1" path="path1"/>
// <siteSetting name="site2" path="path2"/>
// <siteSettings>
// </settings>
// <modules>
// <module name="module1" action="action1">
// <module name="module2" action="action2">
// </modules>
// </configuration>
//----------------------------
//
// However, the XML parser modules I found does something like this (pseudo):
//
// configuration:{
// settings:
// [siteSettings:
// [siteSetting: {name: "site1", path: "path1"},
// siteSetting: {name: "site2", path: "path2"}]};
//
// What do you reccommend - am I even on right track?
// Example parsed XML
var xml = {
configuration: {
settings: [{
siteSettings: [{
siteSetting: {name: "site1", path: "path1"}
},{
siteSetting: {name: "site2", path: "path2"}
}
]
}]
}
};
var rawSiteSettings = xml.configuration.settings[0].siteSettings;
var siteSettings = [];
// Normalize siteSettings from XML
for(var i=0; i<rawSiteSettings.length; i++){
siteSettings.push(rawSiteSettings[i].siteSetting);
}
console.log(siteSettings); // this would equal [{name:"site1"}, {name:"site2"} ect...]
// Helper to search object by value
var getPropertyByName = function(data, value){
for(var prop in data){
if(data.hasOwnProperty(prop)){
if(data[prop].name === value){
return data[prop];
}
}
}
};
var firstSite = getPropertyByName(siteSettings, "site1");
var firstSiteName = firstSite.name;
var firstSitePath = firstSite.path;
console.log('Name: ' + firstSiteName); // site1
console.log('Site Path: ' + firstSitePath); // path1