HI!
I'm creating one simple custom class, which will get data from xml...
The idea is to create fully dinamical class which will get data from xml file
and named every part of array with the attributes, and fill it with its
content...
...hope that make sense...
For example:
part of xml file:
title="sample 1"
part of class code:
for (var j in nodes[i].attributes)
{
content.push({j:nodes[i].attributes[j]});
}
...variable "j" gets the name from the current attribute, inside the current
item(nodes[i].attributes) in the xml file...so I'm using it as a identifier of
the current part of array...
I get the value from the current attributes value(nodes[i].attributes[j])...
At the end I'm adding them to the content
array(content.push({j:nodes[i].attributes[j]}))...
I really hope you understand, what am I trying to say...
...But when I trace the content(I've tried adding the identifiers to its
end('...].title' or something similar))...
I allways get [object Object] or unidentified...
If you have any idea how to do that, please tel me...
...or if it's even possible...
Thanks in advance...
This is my code so far:
import mx.utils.Delegate;
class com.mloncaric.LoaderXML
{
public var onError:Function;
private var xml:XML;
private var numOfItems:Number;
private var container:Array;
public function LoaderXML()
{
}
public function loadXML(targetXML:String):Void
{
this.xml = new XML();
this.container = new Array();
this.xml.ignoreWhite = true;
this.xml.onLoad = Delegate.create(this, onLoadXML);
this.xml.load(targetXML);
}
private function onLoadXML(success:Boolean):Void
{
if (success)
{
var nodes:Array = xml.firstChild.childNodes;
numOfItems = nodes.length;
var content:Object = new Object();
for (var i:Number = 0; i<numOfItems; i++)
{
for (var j in nodes[i].attributes)
{
trace(j+":"+nodes[i].attributes[j]);
content.push({j:nodes[i].attributes[j]});
}
id.shift();
value.shift();
}
}
else
{
onError("Error on loading XML file!");
}
}
}
And in main movie:
Code:
import com.mloncaric.*;
var lxml:LoaderXML = new LoaderXML();
lxml.loadXML("LoaderXML.xml");
lxml.onError = function(error:String):Void
{
trace(error);
}
The sample xml code:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item
title="sample 1"
content="sample content 1"
source="sample1.jpg"
/>
<item
title="sample 2"
content="sample content 2"
source="sample2.jpg"
/>
</items>