actionscript 3 - Parsing XML data in AS3 by attribute -


i building application allows user add ingredients in fridge list , search of potential recipes based on ingredients they've listed.

i using xml data set out in simple schema so:

<items> <item category="meat">chicken</item> <item category="meat">beef</item> <item category="vegetable">green beans</item> </items> 

the scrolling list part of app based on nettuts tutorial (http://active.tutsplus.com/tutorials/actionscript/create-a-smooth-rolling-scroller-with-as3/). want able filter list programatically xml attributes (i.e. 'category' attribute above).

here full document class below (i wasn't sure functions include exclude might important). functions should aware of dataloaded() parses xml data , puts textfield , dataload() instantiates urlloader object load xml. name of document class recipematcher , constructor same.

i think i've commented pretty there shouldn't many question marks on purpose of variables , functions.

package  {     ' imports (all- unsorted) '     import flash.display.movieclip;     import flash.events.mouseevent;     import flash.events.event;     import flash.net.urlrequest;     import flash.net.urlloader;     import flash.text.textformat;     import flash.text.textfield;     import flash.text.textfieldautosize;     import flash.text.font;     import flash.display.shape;     import gs.*;      public class recipematcher extends movieclip     {         'variable declaration'         // ------------------- //         'main structural elements'         private var _pad:movieclip;         public var _addingredient:movieclip;         private var _container:movieclip;         'ingredient wizard structural elements'         public var _ingredientwizard:movieclip;         private var _collapsepanelmeat:movieclip;         private var _collapsepanelveg:movieclip;         private var _collapsepanelcarbs:movieclip;         private var _collapsepanelcuisine:movieclip;         private var _collapsemealtype:movieclip;         private var _collapseother:movieclip;         'data (xml) elements'         public var loader:urlloader;         public var data:xml;         public var items:xmllist;         'positioning of xml data in container - ("text")'         private var _item:item;         private var _itemtextfield:textfield;         private var _defaultformat:textformat = new textformat();         private var _arialrounded:font = new arialrounded();         private var _textfieldxposition:uint = 10;         private var _textfieldyposition:uint = 13;          private var _textfieldwidth:uint = 540;         private var _textfieldpaddingleft:uint = 20;         private var _textfieldheight:uint = 150;         private var _itemposition:uint = 49;         'newingredientswizard positioning'         private var _addnewingredientsxposition:uint = 322;         private var _addnewingredientsyposition:uint = 500;         'input mask - container'         private var _mask:shape;         private var _maskwidth:uint = 580;         private var _maskheight:uint = 540;         private var _paddingtop:uint = 220;         'background container '         private var _background:shape;         'scroll speed properties'         private var _maxspeed:uint = 15;         private var _speed:number;         'end of variable declarations'         //----------------------------------//          //---------constructor function---------//         public function recipematcher()         {             'constructor function recipematcher '             _addingredient = new addingredient();             _ingredientwizard = new ingredientdialog();             _pad = new pad();             addchild(_pad);              _ingredientwizard.visible = false;             _pad.x = stage.stagewidth / 2 - _pad.width / 2;             _pad.y = stage.stageheight / 2 - _pad.height / 2;             'event listener add ingredient button inside of pad movieclip '             _pad.btn_add.addeventlistener(mouseevent.click, addnewingredient);             _container = new movieclip();             addchild(_container);             dataload();         }          //-- beginning of listener functions food category pages - part of ingredient wizard --//          function displaymeat(event:mouseevent):void {             _ingredientwizard.visible = false;         }         function displayveg(event:mouseevent):void {             _ingredientwizard.visible = false;         }         function displaycarbs(event:mouseevent):void {             _ingredientwizard.visible = false;         }         function displaycuisine(event:mouseevent):void {             _ingredientwizard.visible = false;         }         function displaymealtype(event:mouseevent):void {             _ingredientwizard.visible = false;         }         function displayother(event:mouseevent):void {             _ingredientwizard.visible = false;         }          //-- function accompanying function event listener in constructor listens clicks on ingredient items --//          function itemclicked(event:mouseevent):void {             _itemtextfield = new textfield();             trace(_itemtextfield.text);         }         //-- function makes of elements of scrolling list invisible, whilst add new ingredients wizard displayed user //         function addnewingredient(event:mouseevent):void         {             _pad.visible = false;             _container.visible = false;             trace("screen wipe");             _ingredientwizard = new ingredientdialog();             addchild(_ingredientwizard);             _ingredientwizard.x = stage.stagewidth / 2 - _ingredientwizard.width / 2;             _ingredientwizard.y = stage.stageheight / 2 - _ingredientwizard.height / 2;              _ingredientwizard.collapse_meat.btn_submit.addeventlistener(mouseevent.click, displaymeat);             _ingredientwizard.collapse_veg.btn_submit.addeventlistener(mouseevent.click, displayveg);             _ingredientwizard.collapse_carbs.btn_submit.addeventlistener(mouseevent.click, displaycarbs);             _ingredientwizard.collapse_cuisine.btn_submit.addeventlistener(mouseevent.click, displaycuisine);             _ingredientwizard.collapse_mealtype.btn_submit.addeventlistener(mouseevent.click, displaymealtype);             _ingredientwizard.collapse_otherdietarypreferences.btn_submit.addeventlistener(mouseevent.click, displayother);              'submit ingredient choices button listener'             _ingredientwizard.btn_submit.addeventlistener(mouseevent.click, submitingredientchoices);         }          function submitingredientchoices(event:mouseevent):void {             trace("submitted ingredients");          }         private function dataload():void {             'instantiates urlloader object'             loader = new urlloader();             ' listens see if data has loaded'             loader.addeventlistener(event.complete, dataloaded);             'loads local xml file urlrequest method'             loader.load(new urlrequest("data.xml"));         }          private function dataloaded(event:event):void {             ' holds loaded xml data '             data = new xml(event.target.data);             'items properties call - add other calls master properties later on'             items = data.item;             ' tracing name of categories in xml doc'             trace(data.item.@category);             ' parsing of each ingredient'             (var = 0; < items.length(); i++) {                 // instantiation of mcitem (the stage each item)                 _item = new item();                 // sets 'over' layer invisible / transparent //                 _item.item_btn_over.alpha = 0;                 // creates var itemtextfield //                 _itemtextfield = new textfield();                 // _itemtextfield visual attributes //                 _itemtextfield.x = _textfieldxposition + _textfieldpaddingleft;                 _itemtextfield.y = _textfieldyposition;                 _itemtextfield.selectable = true;                 _itemtextfield.wordwrap = true;                 _itemtextfield.width = _textfieldwidth;                 _itemtextfield.height = _textfieldheight;                 _itemtextfield.embedfonts = true;                 _defaultformat.color = 0x111112;                 _defaultformat.font = _arialrounded.fontname;                 _defaultformat.size = 18;                 _itemtextfield.defaulttextformat = _defaultformat;                 _itemtextfield.text = items[i].tostring();                 //adds textfield displaylist//                 _item.addchild(_itemtextfield);                 //vertical positioning//                 _item.y = * _itemposition;                 _item.buttonmode = true;                 _item.mousechildren = false;                 //adds items container displaylist//                 _container.addchild(_item);                 _item.addeventlistener(mouseevent.click, itemclicked);             }                  ' input mask'                 _mask = new shape();                 _mask.graphics.beginfill(0xff0000);                 _mask.graphics.drawrect(0, 0, _maskwidth, _maskheight);                 _mask.graphics.endfill();                 ' positioning of input mask'                 // horizontal centering of input mask//                 _mask.x = stage.stagewidth / 2 - _container.width / 2;                 _mask.y = _paddingtop;                 // adds mask onto stage//                 addchild(_mask);                 // assigns above mask container //                 _container.mask = _mask;                 ' positioning of container mask'                 // horizontal centering of container //                 _container.x = stage.stagewidth / 2 - _container.width / 2;                 // vertical position of container //                 _container.y = _paddingtop;                  'container background stylings'                 _background = new shape();                 _background.graphics.beginfill(0xffffff);                 _background.graphics.drawrect(0, 0, _container.width, _container.height);                 _background.graphics.endfill();                 _container.addchildat(_background, 0);                 'end of container background stylings'                  _container.addeventlistener(mouseevent.mouse_over, movingover);                 _container.addeventlistener(mouseevent.mouse_out, movingout);             }              'function: movingover (mouse_over) event '             private function movingover (event:mouseevent):void {                 _container.removeeventlistener(mouseevent.mouse_over, movingover);                 addeventlistener(event.enter_frame, enterframe);                 if (event.target item)                 tweenmax.to(item(event.target).item_btn_over, .2, {alpha:1});             }             'function: movingout (mouse_out) event '             private function movingout (event:mouseevent):void {                 removeeventlistener(event.enter_frame, enterframe);                 _container.addeventlistener(mouseevent.mouse_over, movingover);                 if (event.target item)                 tweenmax.to(item(event.target).item_btn_over, .2, {alpha:0});             }             'enter_frame listener function gauge scrolling speed based on touch / mouse position + events'             function enterframe(event:event):void {                 _speed = (_mask.height / 2 - _mask.mousey) / (_mask.height / 2) * _maxspeed;                 _container.y += _speed;                 if (_container.y >= _paddingtop) {                     removeeventlistener(event.enter_frame, enterframe);                     _container.y = _paddingtop;                 }                 if (_container.y <= _mask.height - _container.height + _paddingtop) {                     removeeventlistener(event.enter_frame, enterframe);                     _container.y = _mask.height - _container.height + _paddingtop;                 }             }         } } 

answerers, feel free edit code down manageable size once have highlighted functions necessary.

edit: have managed find snippet of code on internet can see in code, lists actual categories rather items in category. i'm not enough @ as3 go further.

 ' tracing name of categories in xml doc'             trace(data.item.@category); 

you must search next line:

const lookingcategoryname : string = "meat"; const findeditemsxmllist : xmllist = data.item.(@category == lookingcategoryname); trace(findeditemsxmllist.toxmlstring()); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -