{lang: 'de'}
Flashhilfe.de - Flash Community

Prototype: XMLSA 1.4 [Flash 8]

 


AntwortenRegistrieren Seite1  

max0matic#1
Benutzerbild von max0matic
Beiträge: 32
Registriert: Jun 2003

29.06.2003, 01:14

XML Simple Array hilft auf einfache und intutive Weise.

Prototype:
ActionScript:
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
/*---------------------------------------------------------------------------
XMLSA for Flash MX v1.4
-----------------------------------------------------------------------------
   Created by
      Max Ziebell (ziebell@worldoptimizer.com)

   Version History:
      06/2003 - Added Mixed-Node notation
              Added optional sessionID and onStartSession, onEndSession-Events
              Added load, send and sendAndLoad
              Added noCache with autodetection if online or local
              changed dump to return the output so you can trace or assign it
              Added dumpHtml
              Added search

      04/2003 - Added insertBefore and removeNode
              removed support for Flash 5! sorry but this was causing problems!

      03/2003 - Added the known methodes of XML to work with XMLSA
                Now you can use: appendChild, cloneNode
              Added removeChildByIndex
   
      01/2003 - Added support for direct access of attributes

      12/2002 - Added support for Flash 5 but in this case I recommend
                to use XMLNitro with it.

      11/2002 - First release
---------------------------------------------------------------------------*/


// Constructor
_global.XMLSA=function ( watchXML ){
   this.$xml = this.$root = this.$parent = new Object();
   ASSetPropFlags(this,null,1,1);
   if (watchXML != undefined) {this._parse.apply (this,arguments)};
}

// load
XMLSA.prototype.load= function(){
   var loader = this._makeLoader(this);
   arguments[0] = this._makeURL(arguments[0]);
   loader.load.apply(loader,arguments);
}

// send
XMLSA.prototype.send= function(){
   arguments[0] = this._makeURL(arguments[0]);
   if(arguments.length==2) {
      this.$root.send.apply(this.$root, arguments);
   } else {
      this.$root.sendAndLoad.apply(this.$root, arguments[0], new XML());
   }
}

// toString
XMLSA.prototype.toString= function(){
   return this.$xml.toString();
}

// sendAndLoad
XMLSA.prototype.sendAndLoad= function(host, target, method){
   var loader = this._makeLoader(target);
   this.$root.sendAndLoad(this._makeURL(host),loader,method);
}

// search
XMLSA.prototype.search = function(criteria, recursive){
   XMLNode.prototype.$criteria = criteria;
   arguments.shift();
   var result = this._search.apply(this,arguments);
   delete (XMLNode.prototype.$criteria);
   return result;
}

//return a reference to XML
XMLSA.prototype.getXML= function(){
   return  this.$xml;
}

// return the value of the firstChild if its a textElement
XMLSA.prototype.getValue= function(){
   return  (this.$xml.firstChild.nodeType == 3 ? this.$xml.firstChild.nodeValue : undefined);
}

// set a textNode
XMLSA.prototype.setValue= function( text ){
   // check if the node has a textElement?
   if (this.$xml.firstChild.nodeType == undefined) {
      // seems like we have to create one...
      this.$xml.appendChild(new XML().createTextNode(text));
      return true;
   // else check if firstChild is a textElement and set it...
    }else if (this.$xml.firstChild.nodeType == 3) {
      this.$xml.firstChild.nodeValue = text;
      return true// retrun success on setValue
    // seams like it ain't possible
   } else {
      return false; // retrun failed on setValue
   }
}

//return the nodeName
XMLSA.prototype.getNodeName= function(){
   return  this.$xml.nodeName;
}

// append a child
XMLSA.prototype.appendChild= function(element){
   if (element instanceof XML) {
      element = element.firstChild;
   }
   this.$xml.appendChild (element);//XML
   this._reParse ();//XMLSA
}

// return a XML object with cloneNode
XMLSA.prototype.cloneNode= function(rekursiv){
   return this.$xml.cloneNode (rekursiv);//XML
}

// this one I added as it's sometimes simpler just want
// to add a new member...
XMLSA.prototype.appendElement= function(name,value,attribs){
   var temp = new XML();
   this.$xml.appendChild(temp.createElement(name));
   if (value != null) {
      this.$xml.lastChild.appendChild (temp.createTextNode(value));
   }
   if (typeof(attribs) == "object" ) {
      // there is a bug in Flash MX so we got to do this...
      // a direct assignment would add __proto__ and constructor
      // to the attributes...
      for (var key in attribs ) {
         this.$xml.lastChild.attributes[key] = attribs[key];
      }
   }
   this._reParse ();//XMLSA
}

// remove child by index
XMLSA.prototype.removeChildByIndex= function(name,idx){
   this[name][idx].$xml.removeNode();
   this[name].splice(idx,1);
}

// remove child by index
XMLSA.prototype.removeNode= function(){
   this.$xml.removeNode();
   this.$parent._reParse();
}

// insert before
XMLSA.prototype.insertBefore= function(node){
   this.$parent.$xml.insertBefore (node,this.$xml);
   this.$parent._reParse();
}

/*************************************************/
// Private
/*************************************************/
// Parser
// called by the constructor and by reParse
XMLSA.prototype._parse= function(node, parent){
   this.$parent = parent;
   // make shure we work with XMLNode
   if (node instanceof XML) {
      this.$version = "XMLSA 1.4";
      this.$root = node;
      node = node.firstChild;
   } else {
      this.$root = this.$parent.$root;
   }
   // store a reference to $xml
   this.$xml=node;
   this.attributes = node.attributes;
   if (node.nodeType == 1 and node.firstChild.nodeType <> 3) {      
      for (var childCounter=0; childCounter< node.childNodes.length; childCounter++) {
         var tempName = node.childNodes[ childCounter ].nodeName;
         if (this[ tempName ]==undefined) {
            this[ tempName ] = new Array();
            this[ tempName ].__resolve = XMLSA.prototype.mixed__resolve;
            ASSetPropFlags(this[ tempName ],null,1,1);
         }
         this[ tempName ].push ( new XMLSA(node.childNodes[ childCounter ], this) );
      }
   }
}

// reParse
// free a brach and reparse it
XMLSA.prototype._reParse= function(){
   this._cleanup();
   // parse it again...
   this._parse (this.$xml,this.$parent);
}

XMLSA.prototype._cleanup= function(){
   // delete all
   for (var found in this){
      delete (this[found]);
   }
}

// make path
XMLSA.prototype._makeURL= function( host ){
   if (this._online()) {
      var nocache = (random(100000)+100000);
      if (_global.sessionID!=undefined) {
         return host+"?sid="+_global.sessionID+"&nocache="+nocache;
      } else {
         return host+"?nocache="+nocache;
      }   
   } else {
      return host;
   }
}

XMLSA.prototype._online = function() {
   // are we online?
   return (_root._url.subStr(0,7) == "http://");
}

// used by send, load and sendAndLoad
XMLSA.prototype._makeLoader= function(target){
   var loader = new XML();
   loader.ignoreWhite = true;
   loader.link = target;
   loader.onLoad = function(ok) {
      if (ok) {
         ASSetPropFlags(this.link,["onLoad"],1,1);
         this.link._cleanup();
         ASSetPropFlags(this.link,["onLoad"],0,1);
         this.link._parse(this);
         this.link.onLoad(true);
         // Experimental Session Support
         //---------------------------------------------------
         // use the attribute 'session' in the root tag to
         // submit a sessionID if you transmit the word
         // 'timeout' or 'end' the session gets deleted
         var header = this.link.attributes;
         if (header.session != undefined) {
            switch (header.session) {
               case "timeout":
               case "end":
                  if (_global.session!=undefined) {
                     delete(_global.session);
                     _global.onSessionEnd(header);
                  }
                  break;
               default:
                  _global.session = new Object();
                  _global.session.id=header.session;
                  _global.onSessionStart(header);
                  break;
            }
         }
      } else {
         this.link.onLoad(false);
      }
   }
   return loader;
}

XMLSA.prototype._search = function(recursive){
   var result = new Array();
   for (var found in this) {
      for (var node in this[found]) {
         if (this[found][node].$xml != undefined) {
            if (this[found][node].$xml.$criteria()) {
               result.push(this[found][node]);
            }
            if (recursive) {
               result = result.concat(this[found][node]._search.apply(this[found][node], arguments));
            }
         }
      }
   }
   return result;
}

// new since 1.4 allows notations without a nodenumber because
// it defaults them to 0 in that case! (redirection)
XMLSA.prototype.mixed__resolve = function(found){
   return this[0][found];
}

/*************************************************/
// Debugger
/*************************************************/
// you can delete this function or use it!
// it provides a nice way to trace XMLSA-Structure
XMLSA.prototype.dump = function ( obj, sName, nLevels) {
   var out="";
   //  init
   if (typeof(obj)!="object") {
      obj = this;
      sName = this.getNodeName();
   }
   var idx, sPre = '';
   if (nLevels == null) {
      nLevels = 0;
   }
   for (idx=1; idx<=nLevels; idx++) {
      sPre += '\t';
   }
   // open branch
   out += sPre + sName + '{\n';
   nLevels++;
   // turn them the right way
   var childs = new Array();
   for (idx in obj) { childs.push(idx) }
   childs.reverse();
   // loop through them
   for (idx=0;idx<childs.length;++idx) {
      if (typeof(obj[childs[idx]]) == 'object' and childs[idx] ne "$xml") {
         out += this.dump( obj[childs[idx]], childs[idx], nLevels );
      } else {
         out += sPre + '\t' + childs[idx] + '="' + obj[childs[idx]].toString() + '"\n';
      }
   }
   // close branch
   out += sPre + '}\n';
   return out;
}

XMLSA.prototype.dumpHTML = function ( obj, sName, nLevels) {
   var out="";
   //  init
   if (typeof(obj)!="object") {
      obj = this;
      sName = this.getNodeName();
   }
   var idx, sPre = '';
   if (nLevels == null) {
      nLevels = 0;
   }
   for (idx=1; idx<=nLevels; idx++) {
      sPre += '&nbsp;';
   }
   // open branch
   out += sPre + sName + '{<br/>';
   nLevels++;
   // turn them the right way
   var childs = new Array();
   for (idx in obj) { childs.push(idx) }
   childs.reverse();
   // loop through them
   for (idx=0;idx<childs.length;++idx) {
      if (typeof(obj[childs[idx]]) == 'object') {
         out += this.dump( obj[childs[idx]], childs[idx], nLevels );
      } else {
         out += sPre + '&nbsp;' + childs[idx] + '="' + obj[childs[idx]].toString() + '"<br/>';
      }
   }
   // close branch
   out += sPre + '}<br/>';
   return out;
}

// hidde all functions to for..in loops
ASSetPropFlags(XMLSA.prototype,null,1,1);


Beispiel Aufruf:
ActionScript:
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
//-------------------------------
// USE THIS XML (portfolio.xml)
//-------------------------------
<portfolio>
  <web>
      <job month="01" year="2002">
        <href>http://www.nike.com</href>
        <desc>this was a job for nike</desc>
      </job>
      <job month="04" year="2002">
        <href>http://www.audi.com</href>
        <desc>this job was made with Flash</desc>
      </job>
  </web>
  <print>
      <job month="14" year="2003">
        <desc>A flyer</desc>
      </job>
  </print>
</portfolio>

//-------------------------------
// INCLUDE
//-------------------------------
#include "XMLSA.as"

//-------------------------------
// LOADING EXAMPLE
//-------------------------------
portfolio = new XMLSA();
portfolio.load("portfolio.xml");
portfolio.onLoad = function(ok){
   if (ok) {
      // do something
      _root.runTest();
   } else {
      // error branch
      trace("error");
   }
}
stop();



function runTest() {
   //-------------------------------
   // EXTRACT VALUES
   //-------------------------------
   trace("EXTRACT VALUES");
   trace( portfolio.web[0].job[0].href[0].getValue() ); // outputs: 'http://www.nike.com'
   trace( portfolio.web[0].job[1].href[0].getValue() ); // outputs: 'http://www.audi.com'
   
   //since this version you can remove '0' because XMLSA 1.5
   //defaults then to '0' so here the same in a shorter way:
   trace( portfolio.web.job.href.getValue() ); // outputs: 'http://www.nike.com'
   trace( portfolio.web.job[1].href.getValue() ); // outputs: 'http://www.audi.com'
   
   //-------------------------------
   // SETTING VALUES
   //-------------------------------
   trace("\nSETTING VALUES");
   portfolio.web.job.href.setValue("http://www.nike-europa.com");
   trace( portfolio.web.job.href.getValue() ); // outputs: 'http://www.nike-europa.com'
   
   //-------------------------------
   // LOOPING
   //-------------------------------
   trace("\nLOOPING");
   for (var i=0; i<portfolio.web.job.length; i++) {
      // outputs: 'http://www.audi.com','http://www.nike-europa.com'
      trace (portfolio.web.job[ i ].href.getValue());
   }
   
   //-------------------------------
   // ACCESSING buildin Flash-XML
   //-------------------------------
   trace("\n$XML-LINK");
   // use the keyword $xml to jump over to the XML-Object
   trace( portfolio.web.job[1].desc.$xml.nodeName ); // outputs: 'desc'
}

Schlagwörter: prototype, xml

AntwortenRegistrieren Seite1  

Schnellantwort

Du musst registriert sein, um diese Funktion nutzen zu können.

 
Ähnliche Beiträge zum Thema
Partner Webseiten: art-and-law.de  Mediengestalter.info   phpwelt.de   Scubacube.de  
Haftungsausschluss   Datenschutzerklärung   Impressum
© 1999-2024 Sebastian Wichmann - Flashhilfe.de