-
[RESOLVED] Read xml from bottom to top
Hi,
i'm trying to read an xml from the bottom so as the latest item is showed in the top in the flash.
the code below works if i'm readin the xml from top to bottom:
Actionscript Code:
function loadArticle(obj) { scrollBttn.lastY = scrollBttn._y; scroll_mc.lastY = scroll_mc._y; scrollBttn._y = scrollBttn.originY; scroll_mc._y = scroll_mc.originY; bttnClose_mc.gotoAndPlay("open"); toScroll = "article_mc"; scroll_mc.article_mc.copy_txt.htmlText = copy_full[obj.ID]; scroll_mc.article_mc.copy_txt._height = scroll_mc.article_mc.copy_txt.textHeight + 20; scroll_mc.article_mc.copy_txt.html = true; scroll_mc.article_mc.copy_txt.styleSheet = styles; scroll_mc.article_mc.headline_txt.text = headline[obj.ID]; scroll_mc.article_mc.date_txt.text = date[obj.ID]; scroll_mc.holder_mc.txt.htmlText = copy_pic[obj.ID]; scroll_mc.holder_mc.txt.html = true; scroll_mc.holder_mc.txt.styleSheet = styles; scroll_mc.holder_mc.gotoAndStop(1); scroll_mc.holder_mc.holder.loadMovie(image_large[obj.ID]); destX = -600; } // End of the function function loadXML(loaded) { if (loaded) { xmlNode = this.firstChild; headline = []; date = []; copy_intro = []; image_thumb = []; copy_full = []; copy_pic = []; image_large = []; total = xmlNode.childNodes.length; headerName.text = xmlNode.attributes.Name; buildNews(); } else { trace ("Error loading XML"); } // end else if } // End of the function stageW = 1000; stageH = 600; scrollTween = 3; scrollSpeed = 8; var styles = new TextField.StyleSheet(); styles.setStyle("a:link", {color: "#C46D4D", textDecoration: "none"}); styles.setStyle("a:hover", {color: "#C46D4D", textDecoration: "none"}); scrollBttn.originY = scrollBttn._y; scrollBttn.originX = scrollBttn._x; scrollBttn.maxY = scrollBar._height - scrollBttn._height + scrollBttn.originY; scroll_mc.originY = scroll_mc._y; originY = scroll_mc._y; originX = scroll_mc._x; scroll_mc.holder_mc.originY = scroll_mc.holder_mc._y; destX = originX; toScroll = "item_mc"; scroll_mc.setMask(mask_mc); scroll_mc.item_mc.item._visible = false; scrollBttn._visible = false; Stage.scaleMode = "noScale"; stageListener = new Object(this); Stage.addListener(stageListener); alignObjects = function () { _parent.bg._width = Stage.width; _parent.bg._height = Stage.height; _parent.bg._x = -(Stage.width - stageW) / 2; _parent.bg._y = -(Stage.height - stageH) / 2; }; stageListener.onResize = function () { alignObjects(); }; alignObjects(); bttnClose_mc.bttnClose.bttn.onRelease = function () { scrollBttn._y = scrollBttn.lastY; scroll_mc._y = scroll_mc.lastY; scroll_mc.holder_mc._y = scroll_mc.holder_mc.originY; bttnClose_mc.gotoAndPlay("close"); toScroll = "item_mc"; destX = originX; }; scrollBar.onPress = function () { jumpTo = _ymouse - scrollBttn._height / 2; if (jumpTo < 0) { scrollBttn._y = 0; } else if (jumpTo > scrollBar._height - scrollBttn._height) { scrollBttn._y = scrollBar._height - scrollBttn._height; } else { scrollBttn._y = jumpTo; } // end else if }; buildNews = function () { for (i = 0; i < total; i++) { image_thumb[i] = xmlNode.childNodes[i].attributes.Thumb; image_large[i] = xmlNode.childNodes[i].attributes.Large; headline[i] = xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue; date[i] = xmlNode.childNodes[i].childNodes[1].firstChild.nodeValue; copy_intro[i] = xmlNode.childNodes[i].childNodes[2].firstChild.nodeValue; copy_full[i] = xmlNode.childNodes[i].childNodes[3].firstChild.nodeValue; copy_pic[i] = xmlNode.childNodes[i].childNodes[4].firstChild.nodeValue; scroll_mc.item_mc.item.duplicateMovieClip("item" + i, i); scroll_mc.item_mc["item" + i].ID = i; scroll_mc.item_mc["item" + i]._y = scroll_mc.item_mc["item" + i]._height * i; scroll_mc.item_mc["item" + i].headline_txt.text = headline[i]; scroll_mc.item_mc["item" + i].date_txt.text = date[i]; scroll_mc.item_mc["item" + i].intro_txt.htmlText = copy_intro[i]; scroll_mc.item_mc["item" + i].intro_txt.html = true; scroll_mc.item_mc["item" + i].intro_txt._height = scroll_mc.item_mc["item" + i].intro_txt.textHeight + 20; scroll_mc.item_mc["item" + i].intro_txt.styleSheet = styles; loadMovie(image_thumb[i], scroll_mc.item_mc["item" + i].holder_mc.holder); scroll_mc.item_mc["item" + i].holder._alpha = 0; } // end of for }; var mouseListener = new Object(this); mouseListener.onMouseWheel = function (wheelNum) { if (scrollBttn._y >= scrollBttn.originY) { scrollBttn._y = scrollBttn._y - wheelNum * scrollRatio * scrollSpeed; if (scrollBttn._y < scrollBttn.originY) { scrollBttn._y = scrollBttn.originY; } else if (scrollBttn._y > scrollBttn.originY + scrollBar._height - scrollBttn._height) { scrollBttn._y = scrollBttn.originY + scrollBar._height - scrollBttn._height; } // end if } // end else if }; Mouse.addListener(mouseListener); onEnterFrame = function () { scroll_mc._x = scroll_mc._x + Math.ceil((destX - scroll_mc._x) / 3); scrollRatio = scroll_mc[toScroll]._height / scrollBar._height; ratio = (scroll_mc[toScroll]._height - scrollBar._height) / (scrollBar._height - scrollBttn._height); if (scroll_mc[toScroll]._height > scrollBar._height) { destScroll = (-scrollBttn._y + scroll_mc.originY) * ratio + scroll_mc.originY; scroll_mc._y = scroll_mc._y + Math.round((destScroll - scroll_mc._y) / scrollTween); scrollBttn._visible = true; } else { scrollBttn._visible = false; } // end else if if (toScroll == "article_mc") { scroll_mc.holder_mc._y = -scroll_mc._y + originY + scroll_mc.holder_mc.originY + scroll_mc.article_mc._y; } // end if }; xmlData = new XML(); xmlData.ignoreWhite = true; xmlData.onLoad = loadXML; xmlData.load("press/content.xml"); stop ();
however i want it to do the opposite, so i've tried changing the for loop to this
Actionscript Code:
for (i = (total-1); i > 0; i--)
but it is still showing the item from top to bottom.
any ideas
-
Senior Member
I dont understand why you want to do that? if you want to manipulate or read the data in special ways..you need to get it into your file first..
for example.. I would suggest you load all data into an ARRAY and then loop through the array backwards... or add to the array in reverse....etc
if the XML is simple enough.. you could get the total chilNodes and loop through in reverse perhaps..
-
thx for the info whispers, but i'm already reading the childnodes
Actionscript Code:
total = xmlNode.childNodes.length;
and then looping in reverse, but that is not working??
how would i load them in to an array?? i'm using this then looping
Actionscript Code:
for (i = 0; i < total; i++) { image_thumb[i] = xmlNode.childNodes[i].attributes.Thumb; }
-
Senior Member
?? this doesnt work for you?
(I made a quick demo)
Actionscript Code:
var totalLinks:Number = 0;
var myXML:XML = new XML(); myXML.ignoreWhite = true; myXML.onLoad = function(success){ if(!success){ trace("XML FAILED TO LOAD!"); }else{ trace("XML LOADED"); totalLinks = myXML.firstChild.childNodes.length; trace(totalLinks); //going forward: for(i=0; i<totalLinks; i++){ trace(i+" - "+myXML.firstChild.childNodes[i]); } trace(newline+"-------[new line]-------"+newline); //going in reverse for(s=totalLinks; s>=0; s--){ trace(s+" - "+myXML.firstChild.childNodes[s]); } } } myXML.load("data.xml");
using this XML:
PHP Code:
<?xml version="1.0"?>
<object> <link>document1-1.pdf</link> <link>document1-2.pdf</link> <link>document1-3.pdf</link> <link>document2-1.pdf</link> <link>document2-3.pdf</link> </object>
if you wanted to dump the data form the XML into an array..
do like so:
var linkArray:Array = new Array();
[and this part goes in your in your onLoad() functn]
//dump to array:
for(e=0; e<totalLinks; e++){
linkArray.push(myXML.firstChild.childNodes[e]);
trace(linkArray[e]);
}
that puts the stuff in the array in the normal order it is in the XML..
you can loop through it in reverse.. (liek we did fo the XML nodes)
you can add to the ARRAY but looping through the XML nodes in reverse.. (hence the Array will be in reverse)..
or you can take any normal array and use the reverse method. which will take an array and reverse the contents..
example:
var numbers_array:Array = new Array(1, 2, 3, 4, 5, 6);
trace(numbers_array); // Displays 1,2,3,4,5,6.
numbers_array.reverse();
trace(numbers_array); // Displays 6,5,4,3,2,1.
-
Thank you, it solved my problem
-
Senior Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|