J'utilise une classe XMLparser pour convertir du XML en objet.Analyse XML avec AS2
Le problème est que le XML que j'ai contient un point dans le nodeName (estate_size.primary_room_area). Bien sûr, cela ne fonctionne pas, car il utilise déjà la notation par points pour le chemin de l'objet.
Quelques idées, mais ont aucune idée comment les faire:
-Remplacer le point au nom d'une certaine manière
-Change le nom.
-Un mieux?
Je ne peux pas utiliser les childNodes natifs et autres, car le XML n'est pas toujours dans le bon ordre. Je n'ai pas accès à l'édition du XML.
Quelqu'un at-il une solution à ce problème?
XML:
<iad>
<DataTag>
<element id="0">
<changed_string>2009-08-20T10:56:00Z</changed_string>
<estate_price.price_suggestion>2500000</estate_price.price_suggestion>
<estate_size.primary_room_area>117</estate_size.primary_room_area>
</element>
</DataTag>
</iad>
AS2:
var xml:XMLParser = new XMLParser();
xml.loadXML("file.xml");
xml.onXMLLoad = function() {
_root.estate_size.text = xml.data.iad.DataTag.element[0].estate_size.primary_room_area;
}
XmlParser:
//import net.za.mediumrare.xmlPackage.XMLParser;
//var myObject:XMLParser = new XMLParser();
//myObject.loadXML ("content.xml");
//myObject.onXMLLoad = function() {
// listAll (myObject.data);
//};
// IMPORTED DEPENDENCIES
//
import mx.utils.Delegate;
import mx.events.EventDispatcher;
//
class XMLParser {
//
// PRIVATE PROPERTIES
//
private var xml:XML;
public var data:Object;
//
//
//
public function get _xml():XML {
return xml;
}
public function set _xml(x:XML) {
xml = x;
}
public function get _data():Object {
return data;
}
public function set _data(o:Object):Void {
trace("ERROR - \"XMLParser\" _data property is read-only and connot be set.");
}
//
// CONSTRUCTOR
//
public function XMLParser(s:String) {
initXML(s);
//data = new Object();
EventDispatcher.initialize(this);
}
//
// PRIVATE METHODS
//
public function buildObject (n){
var o = new String (n.firstChild.nodeValue), s, i, t;
for (s = (o == "null") ? n.firstChild : n.childNodes[1]; s != null; s = s.nextSibling) {
t = s.childNodes.length > 0 ? arguments.callee (s) : new String (s.nodeValue);
for (i in s.attributes) {
t[i] = s.attributes[i];
}
if (o[s.nodeName] != undefined) {
if (!(o[s.nodeName] instanceof Array)) {
o[s.nodeName] = [o[s.nodeName]];
}
o[s.nodeName].push (t);
}
else {
o[s.nodeName] = t;
}
}
data=o;
xml = new XML();
return data;
};
private function initXML(s:String):Void {
if (s == undefined) {
xml = new XML();
xml.ignoreWhite = true;
} else {
xml = new XML(s);
xml.ignoreWhite = true;
}
xml.onLoad = Delegate.create(this, xmlOnLoad);
}
private function xmlOnLoad(success:Boolean):Void {
if (success) {
trace("SUCCESS - xml loaded successfully.");
//xml.ignoreWhite = true;
this.dispatchEvent({type:"onXMLLoad", target:this});
buildObject(xml);
this.onXMLLoad();
} else {
trace("ERROR - xml could not load");
return;
}
}
//
// PUBLIC METHODS
//
public function loadXML(url:String):Void {
xml.load(url);
}
public function getBytesTotal():Number {
return xml.getBytesTotal();
}
public function getBytesLoaded():Number {
return xml.getBytesLoaded();
}
public function getPercentLoaded():Number {
return Math.floor((this.getBytesLoaded()/this.getBytesTotal()) * 100);
}
//
// EVENTS
//
public function onXMLLoad():Void {
// onLoad proxy for internal xml object
}
public function onXMLParse():Void {
// called when xml is finished parsing
}
function addEventListener() {
// Used by EventDispather mixin
}
function removeEventListener() {
// Used by EventDispather mixin
}
function dispatchEvent() {
// Used by EventDispather mixin
}
function dispatchQueue() {
// Used by EventDispather mixin
}
//
}
En fait cela a fonctionné: xml.data.iad.DataTag.element [0] ['estate_size.primary_room_area'], pouvez-vous m'expliquer ce qu'il fait et pourquoi cela fonctionne? –
Parce que les méthodes de traduction dans XMLParser convertissaient tous les nœuds du XML chargé dans un objet. Tout objet dans Actionscript peut être adressé comme un tableau associatif. Donc n'importe quelle propriété peut être accédée comme ça .. par exemple _root ["movieclipName"]. Play() ou même _root.movieclipName ["play"](); – Virusescu