Forum de discussion
Forum « Flash et Shockwave » (archives)
Recursivité en AS 2.0 / passage d'un Object
Envoyé: 1er octobre 2004, 13h13 par Exo
J'essaie de parser un fichier XML et de mettre les valeurs ds un Objet, j'ai dc commencé à écrire une classe XML pour cela.
Je repars d'une fonction ecrite en AS 1.0 par J.W. qui me fait la conversion. L'appel onLoad du XML est fait ds la classe XMLMember qui permet d'appeler une fonction exterieure au onLoad (source bit-101.com)
J'arrive à lire mon fichier XML correctement mais je n'arrive pas à mettre les informations ds mon objet.
Est ce que qqun aurait une aide pour ce soucis ?
Merci d'avance
Exo,
Output panel :
[AS]
[object Object]
XML [object Object]
XML undefined
XML undefined
XML undefined
XML undefined
XML undefined
XML undefined
XML undefined
XML undefined
XML undefined
XML undefined
XML undefined
[/AS]
Ma classe :
[AS]
class managedXML {
/* --- Properties --- */
private var XMLnamefile:String;
private var XMLfile:XML;
public var XMLArray:Object;
/* -------------------------- */
/* --- Methods --- */
/* -- Constructor -- */
public function managedXML (namefile:String) {
this.XMLnamefile = namefile;
this.XMLArray = new Object();
this.init();
}
/* -------------------------- */
/* -- Init function -- */
private function init ():Void {
var tmpXML = new XMLMember(this, "doConvert");
tmpXML.ignoreWhite = true;
tmpXML.load(this.XMLnamefile);
this.toString();
}
/* -------------------------- */
/****
* From XML2AS Class by Justin Watkins
* AS 1.0 convert in AS 2.0
****
*/
/* -- Put XML nodes in an array -- */
public function doConvert (n) {
// arguments n (node), r (object reference)
// vars a (array), d (depth)
var a, d, k;
trace("XML "+this.XMLArray);
// if the nodeName doesn't exist in r create it.
// also keep a reference to just the array and depth
// in case this element is only a text node
if (this.XMLArray[k=n.nodeName] == null) this.XMLArray = ((a=this.XMLArray[k]))[d=0];
// else push a new object on the stack but keep a reference
// to the array and depth in case this element is only a text node
else this.XMLArray = (a=this.XMLArray[k])[d=this.XMLArray[k].push({})-1];
if (n.hasChildNodes()) {
// This element has children and the firstChild is not a text node
if ((k=n.firstChild.nodeType) == 1) {
// map the attributes to the reference and then continue
// the recursion on the children
this.XMLArray.attributes = n.attributes;
for (var i in k=n.childNodes) doConvert(k[i]);
// else this node just stores text
// create a string object so text nodes can have attributes.
} else if (k == 3) {
a[d] = new String(n.firstChild.nodeValue);
a[d].attributes = n.attributes;
}
// this node has no children so map it's attributes.
} else this.XMLArray.attributes = n.attributes;
}
/* -------------------------- */
}
[/AS]
Réponses
|