PDA

View Full Version : [FMX] count and array problem with XML parser



RvGaTe
May 20th, 2003, 03:40 PM
allright, this is the problem, i got this xml file...



<portfolio>
<item>
<img>blaat.jpg</img>
<text>dit is het eerste stukje in men portfolio</text>
<url>http://www.campzone.nl/</url>
</item>
<item>
<img>blaat.jpg</img>
<text>dit is het eerste stukje in men portfolio</text>
<url>http://www.campzone.nl/</url>
</item>
<item>
<img>blaat.jpg</img>
<text>dit is het eerste stukje in men portfolio</text>
<url>http://www.campzone.nl/</url>
</item>
<item>
<img>blaat.jpg</img>
<text>dit is het eerste stukje in men portfolio</text>
<url>http://www.campzone.nl/</url>
</item>
<item>
<img>blaat.jpg</img>
<text>dit is het eerste stukje in men portfolio</text>
<url>http://www.campzone.nl/</url>
</item>
<item>
<img>blaat.jpg</img>
<text>dit is het eerste stukje in men portfolio</text>
<url>http://www.campzone.nl/</url>
</item>
<item>
<img>blaat.jpg</img>
<text>dit is het eerste stukje in men portfolio</text>
<url>http://www.campzone.nl/</url>
</item>
<item>
<img>blaat.jpg</img>
<text>dit is het eerste stukje in men portfolio</text>
<url>http://www.campzone.nl/</url>
</item>
<item>
<img>blaat.jpg</img>
<text>dit is het eerste stukje in men portfolio</text>
<url>http://www.campzone.nl/</url>
</item>
</portfolio>

and i need to load the info into an simple array called
portData[]

wich will be a 2 demi array like this,

portData[itemnumber]["img"]
portData[itemnumber]["txt"]
portData[itemnumber]["url"]

sounds easy.... so i gave it a try,


portfolioXML = new XML();
portfolioXML.load(portfoliofile);
function loadPortfolio() {
secondList = new Array();
dataList = portfolioXML.firstChild.childNodes;
i = 0;
while (i<dataList.length) {
if (dataList[i].nodeName.toLowerCase() == "item") {
secondcont = dataList[i];
secondList = secondcont.childNodes;
ii = 0;
while (ii<secondList.length) {
newslist[i] = new Array();
if (secondList[ii].nodeName != null) {
if (secondList[ii].nodeName == "img") {
trace(i+"___"+secondList[ii].childNodes);
} else if (secondList[ii].nodeName == "text") {
trace(i+"___"+secondList[ii].childNodes);
} else if (secondList[ii].nodeName == "url") {
trace(i+"___"+secondList[ii].childNodes);
trace("");
}
}
ii++;
}
}
i++;
}
}
portfolioXML.onLoad = loadPortfolio;

now, this should work, BUT it doz it all wrong, if you let the script run, it comes up with these traces:


1___blaat.jpg
1___dit is het eerste stukje in men portfolio
1___http://www.website.com/

3___blaat.jpg
3___dit is het eerste stukje in men portfolio
3___http://www.website.com/

5___blaat.jpg
5___dit is het eerste stukje in men portfolio
5___http://www.website.com/

7___blaat.jpg
7___dit is het eerste stukje in men portfolio
7___http://www.website.com/

9___blaat.jpg
9___dit is het eerste stukje in men portfolio
9___http://www.website.com/

11___blaat.jpg
11___dit is het eerste stukje in men portfolio
11___http://www.website.com/

13___blaat.jpg
13___dit is het eerste stukje in men portfolio
13___http://www.website.com/

15___blaat.jpg
15___dit is het eerste stukje in men portfolio
15___http://www.website.com/

17___blaat.jpg
17___dit is het eerste stukje in men portfolio
17___http://www.website.com/

this is totaly wrong! for some reason, the script skips the loop each time.

anyways, i thought, what the heck, lets try to load it into an array, so i replaced the previeus code, to let the values apply on a array, code changed to:


portfolioXML = new XML();
portfolioXML.load(portfoliofile);
function loadPortfolio() {
secondList = new Array();
_global.portData = new Array();
dataList = portfolioXML.firstChild.childNodes;
i = 0;
while (i<dataList.length) {
if (dataList[i].nodeName.toLowerCase() == "item") {
_global.portData[i] = new Array();
secondcont = dataList[i];
secondList = secondcont.childNodes;
ii = 0;
while (ii<secondList.length) {
newslist[i] = new Array();
if (secondList[ii].nodeName != null) {
if (secondList[ii].nodeName == "img") {
_global.portData[i]["img"] = secondList[ii].childNodes
} else if (secondList[ii].nodeName == "text") {
_global.portData[i]["text"] = secondList[ii].childNodes
} else if (secondList[ii].nodeName == "url") {
_global.portData[i]["url"] = secondList[ii].childNodes
}
}
ii++;
}
}
i++;
}
trace(_global.portData)
}
portfolioXML.onLoad = loadPortfolio;
portfolioXML.onLoad = play();

wich will load them into an array, but when you run the script, the trace is:


,,,,,,,,,,,,,,,,,

and when i trace it OUTSIDE the function, it gives me undefind.
i mean, wtf ? what did i do wrong...

could anyone, pleaase, help me with this, im gettin frustrated here ! :angry: :angry:

RvGaTe
May 20th, 2003, 06:34 PM
anyone ?

RvGaTe
May 21st, 2003, 02:10 PM
thx for your help guys, but i already got it fixed :sigh:

Jubba
May 21st, 2003, 02:12 PM
Sorry man, I know nothing when it comes to XML. what was the fix?

RvGaTe
May 21st, 2003, 02:15 PM
portfolioXML = new XML();
portfolioXML.load(portfoliofile);
function loadPortfolio() {
if (_global.stage_webvar) {
randomvar = "?"+int(Math.round(Math.random()*100000));
} else {
randomvar = ""
}
secondList = new Array();
_global.portData = new Array();
dataList = portfolioXML.firstChild.childNodes;
i = 0;
while (i<dataList.length) {
if (dataList[i].nodeName.toLowerCase() == "item") {
secondcont = dataList[i];
secondList = secondcont.childNodes;
ii = 0;
_global.newnumber = _global.portData.length;
_global.portData[_global.newnumber] = new Array();
_global.aantal = _global.portData.length;
while (ii<secondList.length) {
newslist[i] = new Array();
if (secondList[ii].nodeName != null) {
if (secondList[ii].nodeName == "img") {
_global.portData[_global.newnumber]["img"] = folder+secondList[ii].childNodes+randomvar;
} else if (secondList[ii].nodeName == "text") {
_global.portData[_global.newnumber]["text"] = secondList[ii].childNodes;
} else if (secondList[ii].nodeName == "url") {
_global.portData[_global.newnumber]["url"] = secondList[ii].childNodes;
}
}
ii++;
}
}
i++;
}
buildLayout();
play();
}


i just applied the array different :) so it would count upwards like 1 2 3 4 5, and the i's and ii's will just keep skipping like 1 3 5 7

kinda complex, but if you take a closer look, its actualy pretty simple