Bonjour à tous,
voilà j'ai un petit soucis de compréhension avec JSon.
Je dois remplir un champ textarea avec certaines données.
Pour certaines des valeurs je n'ai pas de soucis, mais pour d'autres je n'y arrive pas du tout
Voici le fichier html avec le code javascript
Voici le fichier xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425 <html> <head> <script type="text/javascript" src="softxmllib.js"></script> <script type="text/javascript"> var http = false; function getHTTPObject() { var xmlhttp = false; if (window.ActiveXObject) { try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } } else if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { xmlhttp = new XMLHttpRequest(); if (xmlhttp.overrideMimeType) xmlhttp.overrideMimeType('text/xml'); if (!xmlhttp) xmlhttp = null; } return xmlhttp; } function getNewRelease() { var urlic = "1109542.xml"; http = getHTTPObject(); http.onreadystatechange = getReleaseInfo; http.open("GET", urlic, true); http.send(null); } function getReleaseInfo() { var form = ''; if (http.readyState == 4) if (http.status == 200) { var xmlRelease = new SoftXMLLib(); xmlRelease.loadXML(http.responseText); if (xmlRelease.loadXMLError != 0) { alert("XML file is not valid!"); } else { form = form + "Artist: "; form = form + getArtists(xmlRelease.selectNodes("/resp/release/artists/artist")); form = form + "\n"; form = form + "Album: "; form = form + getAlbum(xmlRelease.selectNodes("/resp/release//title/text()")); form = form + "\n"; form = form + "Discogs: "; form = form + getDiscogsLink(xmlRelease.selectNodes("/resp/release")); form = form + "\n"; form = form + "Label: "; form = form + getLabel(xmlRelease.selectNodes("/resp/release/labels/label")); form = form + "\n"; form = form + "Catalog#: "; form = form + getCatalog(xmlRelease.selectNodes("/resp/release/labels/label")); form = form + "\n"; form = form + "Released: "; form = form + getReleased(xmlRelease.selectNodes("/resp/release/released/text()")); form = form + "\n"; form = form + "Style: "; form = form + getStyle(xmlRelease.selectNodes("/resp/release/styles/style/text()")); form = form + "\n"; form = form + "Quality: "; form = form + "\n\n"; form = form + "Tracklist: "; form = form + getTracklist(xmlRelease.selectNodes("/resp/release/tracklist/track")); form = form + "\n"; document.getElementById("text").value = form; } } else { alert('Server is too busy or not responding in time; please try again later.'); } } function getArtists(artistNode) { var artists = ""; var nodes = artistNode[0].self.childNodes; for (var j = 0; j < nodes.length; j++) { var artistName = ""; var anv = ""; var join = ""; var node = nodes[j].childNodes; for (var k = 0; k < node.length; k++) { var n = node[k]; if (n.nodeName == "name") { artistName = n.childNodes[0].nodeValue; } if (n.nodeName == "anv") { anv = n.childNodes[0].nodeValue; } if (n.nodeName == "join") { join = n.childNodes[0].nodeValue; } } if (anv.length == 0 && anv == "") { artists = artists + artistName + " "; } else artists = artists + anv + " "; if (join.length > 0) { artists = artists + join + " "; } } return artists.replace(/^\s+/g, '').replace(/\s+$/g, ''); } function getDiscogsLink(releaseId) { return "http://www.discogs.com/release/"+ releaseId[0].attributes[0].id; } function getAlbum(albumNode) { return albumNode[0].self.nodeValue; } function getLabel(labelNode) { var label = ""; for (var i = 0; i < labelNode.length; i++) { label = label + labelNode[i].attributes[0].name + " / "; } return label.substring(0,label.lastIndexOf("/")).replace(/^\s+/g, '').replace(/\s+$/g, ''); } function getCatalog(catalogNode) { var label = ""; for (var i = 0; i < catalogNode.length; i++) { label = label + catalogNode[i].attributes[0].catno + " / "; } return label.substring(0,label.lastIndexOf("/")).replace(/^\s+/g, '').replace(/\s+$/g, ''); } function getReleased(releasedNode) { return releasedNode[0].self.nodeValue; } function getStyle(styleNode) { var style = ""; for (var i = 0; i < styleNode.length; i++) { style = style + styleNode[i].self.nodeValue + " / "; } return style.substring(0,style.lastIndexOf("/")).replace(/^\s+/g, '').replace(/\s+$/g, ''); } function getTracklist(tracklistNode) { var tracklist = ""; alert(tracklistNode.length); //alert(matchedTracks[0].self.childNodes[0].childNodes[0].nodeName); //for (var i = 0; i < tracklistNode.length; i++) //{ var trackChilds = tracklistNode[0].self.childNodes; var position = ''; var artist = ''; var title = ''; var duration = ''; for (var j = 0; j < trackChilds.length; j++) { //alert(trackChilds[j].childNodes[0].nodeName); var nodes = trackChilds[j].childNodes; alert(nodes.length); for (var z = 0; z < nodes.length; z++) { var node = nodes[z]; if (node.nodeName == "position") { position = node.childNodes[0].nodeValue; } else if (node.nodeName == "artists") { var artistNode = node.childNodes; for (var k = 0; k < artistNode.length; k++) { var artistChildNode = artistNode[k].childNodes; var art = ""; var anv = ""; for (var l = 0; l < artistChildNode.length; l++) { if (artistChildNode[l].nodeName == "name") { art = artistChildNode[l].childNodes[0].nodeValue; } if(artistChildNode[l].nodeName == "anv") { anv = artistChildNode[l].childNodes[0].nodeValue; } artist = artist + (anv.length > 0 ? anv : art) + " / "; } } artist = artist.substring(0, artist.lastIndexOf("/")); } else if (node.nodeName == "title") { title = node.childNodes[0].nodeValue; } else if (node.nodeName == "duration") { duration = '(' + node.childNodes[0].nodeValue + ')'; } } tracklist = tracklist + position + " " + artist + " - " + title + " " + duration + "\n"; } //} return tracklist.substring(0,tracklist.lastIndexOf("/")).replace(/^\s+/g, '').replace(/\s+$/g, ''); //alert(tracklist); } function loadRelease() { //var urlic = "http://www.torrentech.org/myadmin/discogs.php?id=" + document.getElementById('ajdi').value; http = getHTTPObject(); var urlic = "http://10.1.1.29/Torrentech/resources/1506005.xml"; //ne moze ovo jer se dobija permission denied... ne moze ajax da ucitava nista sto nije sa domena gde je on sam //var urlic = "http://www.discogs.com/release/" + document.getElementById('ajdi').value + "?f=xml&api_key=59d9b19e15"; http.open("GET", urlic, true); http.onreadystatechange = function() { if (http.readyState == 4) { if (http.status == 200) { var matchedNodes; var xmlData = new SoftXMLLib(); xmlData.load(http.responseXML); if (xmlData.loadXMLError != 0) { alert("XML file is not valid!"); } else { //Artisti: var xpath = "/resp/release/artists/artist/name/text()"; var form = "Artist: "; matchedNodes = xmlData.selectNodes(xpath); var b = matchedNodes.length - 1; for (var i = 0; i < matchedNodes.length; i++) { form = form + matchedNodes[i].self.nodeValue; //ako nije zadnji element... if (i != b) form = form + " / "; } form = form + "\n"; //Album: xpath = "/resp/release//title/text()"; matchedNodes = xmlData.selectNodes(xpath); form = form + "Album: " + matchedNodes[0].self.nodeValue + "\n"; //Discogs: xpath = "//resp/release"; matchedNodes = xmlData.selectNodes(xpath); form = form + "Discogs: http://www.discogs.com/release/" + matchedNodes[0].attributes[0].id + "\n"; } //Label: xpath = "/resp/release/labels/label"; matchedNodes = xmlData.selectNodes(xpath); var b = matchedNodes.length - 1; for (var i = 0; i < matchedNodes.length; i++) { form = form + "Label: " + matchedNodes[i].attributes[0].name; //ako nije zadnji element... if (i != b) form = form + " / "; } form = form + "\n"; //Catno: matchedNodes = xmlData.selectNodes(xpath); form = form + "Catalog#: " + matchedNodes[0].attributes[0].catno + "\n"; //Released: xpath = "/resp/release/released/text()"; matchedNodes = xmlData.selectNodes(xpath); form = form + "Released: " + matchedNodes[0].self.nodeValue + "\n"; //Style: var xpath = "/resp/release/styles/style/text()"; form = form + "Style: "; matchedNodes = xmlData.selectNodes(xpath); b = matchedNodes.length - 1; for (var i = 0; i < matchedNodes.length; i++) { form = form + matchedNodes[i].self.nodeValue; //ako nije zadnji element... if (i != b) form = form + " / "; } form = form + "\n"; form = form + "Quality: " + "\n\n"; //Tracklist: xpath = "/resp/release/tracklist/track"; var tracklist = ""; var tempTrack = ""; var matchedTracks = xmlData.selectNodes(xpath); alert(matchedTracks.length); //alert(matchedTracks[0].self.childNodes[0].childNodes[0].nodeName); for (var i = 0; i < matchedTracks.length; i++) { var trackChilds = matchedTracks[i].self.childNodes; var position = ''; var artist = ''; var title = ''; var duration = ''; for (var j = 0; j < trackChilds.length; j++) { //alert(trackChilds[j].childNodes[0].nodeName); var nodes = trackChilds[j].childNodes; for (var z = 0; z < nodes.length; z++) { var node = nodes[z]; if (node.nodeName == "position") { position = node.childNodes[0].nodeValue; } else if (node.nodeName == "artists") { var artistNode = node.childNodes; for (var k = 0; k < artistNode.length; k++) { var artistChildNode = artistNode[k].childNodes; for (var l = 0; l < artistChildNode.length; l++) { if (artistChildNode[l].nodeName == "name") { artist = artist + artistChildNode[l].childNodes[0].nodeValue + " / "; } } } artist = artist.substring(0, artist.lastIndexOf("/")); } else if (node.nodeName == "title") { title = node.childNodes[0].nodeValue; } else if (node.nodeName == "duration") { duration = '(' + node.childNodes[0].nodeValue + ')'; } } tracklist = tracklist + position + " " + artist + " - " + title + " " + duration + "\n"; } } form = form + tracklist; alert(form); //alert(tracklist); } //od else iz linije 30 } } http.send(null); } </script> </head> <body> <table> <tr><td> <input type="text" id="ajdi" value="2"/> <input type="button" onclick="getNewRelease()" name="submitId" value="populate from Discogs"/> </td><td></td></tr> <tr><td colspan="2"><textarea rows="40" cols="100" id="text"></textarea></td></tr> </table> <div id="foo"> </div> </body> </html>
Et enfin la librairie (en pièce jointe) qui permet de parser le xml pour le transformer en code lisible par JSon.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355 <?xml version="1.0" encoding="utf-8"?> <!-- Created with Liquid XML Studio 1.0.8.0 (http://www.liquid-technologies.com) --> <resp stat="ok" version="1.0" requests="4"> <release id="1109542" status="Accepted"> <images> <image height="600" type="primary" uri="http://www.discogs.com/image/R-1109542-1192735422.jpeg" uri150="http://www.discogs.com/image/R-150-1109542-1192735422.jpeg" width="581" /> <image height="600" type="secondary" uri="http://www.discogs.com/image/R-1109542-1192735435.jpeg" uri150="http://www.discogs.com/image/R-150-1109542-1192735435.jpeg" width="580" /> </images> <artists> <artist> <name>Various</name> </artist> </artists> <title>2OO</title> <labels> <label catno="ZIQ200CD" name="Planet Mu" /> </labels> <formats> <format name="CDr" qty="2"> <descriptions> <description>Compilation</description> <description>Promo</description> <description>Album</description> </descriptions> </format> </formats> <genres> <genre>Electronic</genre> </genres> <styles> <style>IDM</style> <style>Dubstep</style> <style>Breakcore</style> <style>Acid</style> </styles> <country>UK</country> <released>2007-10-08</released> <notes>Celebrating Planet Mu's 10th birthday and 200th release. In PVC slipcase with printed insert.CD1/CD2 written on CD-R's in marker pen.</notes> <tracklist> <track> <position>1.01</position> <artists> <artist> <name>Ceephax Acid Crew</name> <anv>Ceephax</anv> </artist> </artists> <title>Castilian</title> <duration>4:54</duration> </track> <track> <position>1.02</position> <artists> <artist> <name>Darqwan</name> </artist> </artists> <title>M/A.. Ximum Reespek</title> <duration>5:37</duration> </track> <track> <position>1.03</position> <artists> <artist> <name>Boxcutter</name> </artist> </artists> <title>Good You Dub</title> <duration>4:52</duration> </track> <track> <position>1.04</position> <artists> <artist> <name>Gasman, The</name> </artist> </artists> <title>Equino</title> <duration>4:51</duration> </track> <track> <position>1.05</position> <artists> <artist> <name>Ital Tek</name> </artist> </artists> <title>Whitemark</title> <duration>4:18</duration> </track> <track> <position>1.06</position> <artists> <artist> <name>Parson (2)</name> </artist> </artists> <title>Throw Some Ds</title> <duration>4:41</duration> </track> <track> <position>1.07</position> <artists> <artist> <name>Bizzy B</name> <join>&</join> </artist> <artist> <name>Equinox (3)</name> </artist> </artists> <title>Merda Style 2004</title> <duration>5:11</duration> </track> <track> <position>1.08</position> <artists> <artist> <name>Syntheme</name> </artist> </artists> <title>Finnial (Chevron's Acid Sandwich Bhuna Mix)</title> <extraartists> <artist> <name>Chevron</name> <role>Remix</role> </artist> </extraartists> <duration>3:03</duration> </track> <track> <position>1.09</position> <artists> <artist> <name>Venetian Snares</name> </artist> </artists> <title>Devil's Totem</title> <duration>4:23</duration> </track> <track> <position>1.10</position> <artists> <artist> <name>Milanese</name> <join>Feat.</join> </artist> <artist> <name>Anneka</name> </artist> </artists> <title>Reading You</title> <extraartists> <artist> <name>Anneka</name> <role>Featuring</role> </artist> </extraartists> <duration>2:26</duration> </track> <track> <position>1.11</position> <artists> <artist> <name>Julian Fane</name> </artist> </artists> <title>The Moon Is Gone</title> <duration>4:33</duration> </track> <track> <position>1.12</position> <artists> <artist> <name>DJ Distance</name> <anv>Distance</anv> </artist> </artists> <title>Fallen (Vex'd Remix)</title> <extraartists> <artist> <name>Vex'd</name> <role>Remix</role> </artist> </extraartists> <duration>6:18</duration> </track> <track> <position>1.13</position> <artists> <artist> <name>Ambulance</name> </artist> </artists> <title>The Tams</title> <duration>6:19</duration> </track> <track> <position>2.01</position> <artists> <artist> <name>Pinch (2)</name> </artist> </artists> <title>Punisher (Loefah's SE25 Remix)</title> <extraartists> <artist> <name>Loefah</name> <role>Remix</role> </artist> </extraartists> <duration>5:36</duration> </track> <track> <position>2.02</position> <artists> <artist> <name>Luke Vibert</name> </artist> </artists> <title>Acid Meters</title> <duration>3:45</duration> </track> <track> <position>2.03</position> <artists> <artist> <name>FFF</name> </artist> </artists> <title>The Feeling</title> <duration>5:43</duration> </track> <track> <position>2.04</position> <artists> <artist> <name>Pinch (2)</name> </artist> </artists> <title>Pepper Spray</title> <duration>4:49</duration> </track> <track> <position>2.05</position> <artists> <artist> <name>Shitmat</name> </artist> </artists> <title>Shut Up (Hellfish Remix)</title> <extraartists> <artist> <name>Hellfish</name> <role>Remix</role> </artist> </extraartists> <duration>5:41</duration> </track> <track> <position>2.06</position> <artists> <artist> <name>Tim Exile</name> <anv>Exile</anv> <join>&</join> </artist> <artist> <name>John B</name> </artist> </artists> <title>Broken Language (Exile Mix)</title> <extraartists> <artist> <name>Tim Exile</name> <anv>Exile</anv> <role>Remix</role> </artist> </extraartists> <duration>5:38</duration> </track> <track> <position>2.07</position> <artists> <artist> <name>Doubtful Guest, The</name> </artist> </artists> <title>Nannita</title> <duration>7:11</duration> </track> <track> <position>2.08</position> <artists> <artist> <name>µ-Ziq</name> </artist> </artists> <title>Lexicon</title> <duration>4:16</duration> </track> <track> <position>2.09</position> <artists> <artist> <name>Jo Apps</name> </artist> </artists> <title>Kausikan</title> <duration>4:21</duration> </track> <track> <position>2.10</position> <artists> <artist> <name>Neil Landstrumm</name> </artist> </artists> <title>Bleep Biopsy</title> <duration>4:36</duration> </track> <track> <position>2.11</position> <artists> <artist> <name>Benga</name> </artist> </artists> <title>Broken Dubstep</title> <duration>5:33</duration> </track> <track> <position>2.12</position> <artists> <artist> <name>Duran Duran Duran</name> </artist> </artists> <title>Face Blast</title> <duration>3:53</duration> </track> <track> <position>2.13</position> <artists> <artist> <name>Mark One</name> <anv>MRK 1</anv> </artist> </artists> <title>Sensi Skank</title> <duration>4:13</duration> </track> </tracklist> </release> </resp>
Tout se passe excepté pour la création de la tracklist.
Voici ce qu'il devrait me retourner
En exécutant le code vous remarquerez qu'il ne me retourne pas du tout le résultat escompté.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 1.01 Ceephax Castilian (4:54) 1.02 Darqwan M/A.. Ximum Reespek (5:37) 1.03 Boxcutter Good You Dub (4:52) 1.04 Gasman, The Equino (4:51) 1.05 Ital Tek Whitemark (4:18) 1.06 Parson (2) Throw Some Ds (4:41) 1.07 Bizzy B & Equinox (3) Merda Style 2004 (5:11) 1.08 Syntheme Finnial (Chevron's Acid Sandwich Bhuna Mix) (3:03) 1.09 Venetian Snares Devil's Totem (4:23) 1.10 Milanese Feat. Anneka Reading You (2:26) 1.11 Julian Fane The Moon Is Gone (4:33) 1.12 Dstance Fallen (Vex'd Remix) (6:18) 1.13 Ambulance The Tams (6:19) 2.01 Pinch (2) Punisher (Loefah's SE25 Remix) (5:36) 2.02 Luke Vibert Acid Meters (3:45) 2.03 FFF The Feeling (5:43) 2.04 Pinch (2) Pepper Spray (4:49) 2.05 Shitmat Shut Up (Hellfish Remix) (5:41) 2.06 Exile & John B Broken Language (Exile Mix) (5:38) 2.07 Doubtful Guest, The Nannita (7:11) 2.08 µ-Ziq Lexicon (4:16) 2.09 Jo Apps Kausikan (4:21) 2.10 Neil Landstrumm Bleep Biopsy (4:36) 2.11 Benga Broken Dubstep (5:33) 2.12 Duran Duran Duran Face Blast (3:53) 2.13 MRK 1 Sensi Skank (4:13)
Si une âme charitable pourrait m'aiguiller, cela serait très gentil.
D'avance merci pour vos réponses.
Partager