A Flash Developer Resource Site

Page 1 of 3 123 LastLast
Results 1 to 20 of 50

Thread: using XML and movieClips/Objects?

  1. #1
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756

    using XML and movieClips/Objects?

    Im not really sue how to go about this..

    assuming this is a valid XML document:

    code:

    <products>
    <product1>
    <image>image1.jpg</image>
    <name>Example Shirt 1</name>
    <price>$19.99</price>
    <size>small</size>
    <description>etc..etc..etc.</description>
    </product1>

    <product2>
    <image>image2.jpg</image>
    <name>Example Shirt 2</name>
    <price>$24.99</price>
    <size>medium</size>
    <description>etc..etc..etc.</description>
    </product1>
    </products>



    I have never used or played with XML before...anywhere...whether in HTML(schemas) or in/through FLASH.....but sort liek loading a varObject correct? then pathing to whatever node/element I need?

    1.) Can FLASH actually create or EDIT an XML document? or is it the same rules that apply to text files (read only) (needs to be prepared prior)

    2.) My main focus of this is:

    I would like to just be able to edit/update the XML file to add more products (same layout as above).. and have FLASH read how many products there are(an array Im assuming) ..and create an OBJECT (productObject) using the info INSIDE each of the product NODES. (I guess using attachMovieClip or duplicateMovieClip?)

    So I would like to have a base MOVIECLIP or OBJECT that has a little visual layout that is STATIC for EVERY product and DUPLICATE this for EACH product NODE...."AS WELL AS" have each product NODE match the details in that element

    So FLASH reads the XML file..sees ther is 3 products 1, 2 and 3...

    It will then create (duplicate??) this BASE object modle hat has the same BG color, same tex, and graphics...but then take the value in the <image> element and place this inside the productObject...same for price, name & desc. ...then move onto product node 2 and do the same thing to poulate a NEWLY created clip (object ?)

    I know this needs (for me at least) to be tackled in several steps... but would like some oversight on where to get started on this.

    Would I need to use some sort of array.length property to get how many "productNodes" there are...and then either attach or duplicate a premade "shell" movieClip to match that number..?

    never used much of either (attach or duplicate)...so would I go about populating these "dups" with the new characteristics of each node in the XML document? (product elements)

    heres a link to an image of the type of "SHELL" I want to create, then duplicate (and then ultimately, populate with the values from the XML document)

    http://www.flashkit.com/board/attach...chmentid=37353

    IMAGE (being a blankClip that gets an .jpg loaded into it.)

    Name
    Price
    Description

    are all dynamic text fields being, and 4 of these get their content form the XML file (nodes)

    The DropDown Color & DropDown Size are pre-made (static value) drop down boxes or components...and the buy button (also never changing) that grabs all the values and sends them off to "wherever..paypal for all I care for now)

    any help is appreciated

    Thanks

  2. #2
    Senior Member tupps's Avatar
    Join Date
    Jan 2001
    Location
    Melbourne
    Posts
    2,035
    Flash can create and edit xml files. However flash cannot save the files anywhere without the help of some backend scripting. So whatever the solution you will need a script to save the files to disk.

    What you describe shouldn't be to hard. One thing to remember when you are working with XML that it is just a bunch of arrays with some extra functions.

    Thanks

    Luke
    Flash-XML FAQ
    http://www.tupps.com/flash/faq/

  3. #3
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    Hey tupps-

    Thanks for the reply.

    Maybe you can start me off here (or somewhere).. as to maybe the first step to approaching / reaching this?


    so..ok..

    1.) Make my XML file in the outlined fashion above..(is that OK?)
    2.) then use an XML object to load (possibly "endAndLoad") the NODES into my XML object....(right?)


    here is where I get lost.. I am NO coder, by any means...(doubtful if Im script kiddie status!)

    Once I get my "arrays"/XML Nodes/data into my XML object...

    Was I correct? in using XMLobject.length or something?

    Duplicate?..or attachMovieClip?

    I guess the VAGUE overlying question is:

    1.) how to DYNAMICALLY create an Object (is that even the write wording to use) or MovieClip (that all have similar, static quialities)

    2.) take these (however may) dynamically created "OBJETS" and populate them with unique info form each XML node elements.?


    Doesnt seem like it would be THAT much CODE..Im not very strong with ARRAYS, LOOPS...and especially attachMovieClip or duplicateMovieClip... not to mention NEVER used XML before (at all)..but working my way through and accessing the nodes/elements I think I can handle..

    So any tips?

  4. #4

  5. #5
    Banned indivision's Avatar
    Join Date
    Jul 2001
    Location
    Portland, OR
    Posts
    474
    it seems to me that you should focus first on getting comfortable with arrays, loops, attachMovie, etc. once you do, you will have a basis to understand how to handle XML. as it is, i think your lack of familiarity with those programming concepts gets in the way of you learning how to use XML.

    i would suggest starting by putting together an fla that has a hard-coded array in it and a loop that attaches a movieclip per element in the array. give it a try with the macromedia documentation first. i can give a description of how to do it but familiarizing yourself with how to get answers for native aspects of the language is also vital.

    to answer how people use XML. macromedia provides XML parsing. however, myself and a lot of people find the interface for their xml parser to be somewhat awkward. the most popular tool used to deal with that issue is xpath ( http://www.xfactorstudio.com/Actionscript/AS2/XPath/ ) which basically treats an xml file almost like a database that you can perform detailed searches on to retrieve the data you want. its a good solution because it only parses the xml that your program needs. other people use xml parsers that turn xml into a dot syntax multidimensional object that matches the xml structure. personally, i use the xml parser solution as its easier to parse it all once and i rarely work with xml files that are large enough to make a substantial performance difference between parsing what you need and just parsing it all. i can send you my xml parser if you're interested.
    Last edited by indivision; 04-01-2006 at 03:31 AM.

  6. #6
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    i would suggest starting by putting together an fla that has a hard-coded array in it and a loop that attaches a movieclip per element in the array. give it a try with the macromedia documentation first. i can give a description of how to do it but familiarizing yourself with how to get answers for native aspects of the language is also vital.
    Well I haved worked with arrays..and accessing the data in the array... and Im sure I can figure out a loop..using something I have dealt with before..

    code:

    //product Array
    var product1Array = [];
    product1Array = loadedProducts.product1.split(",");

    for (i=0; i<product1Array.length; i++) {
    //trace(product1Array[i]);




    loadedProducts: being the XML object that loads the XML file (I know this is for a varObject example)
    product1: being the first node in the products XML file..


    as far accessing the XML attributes...I think its fairly straight forward childNode, nextSibling..etc..


    So I guess.. I need to know about the attach or duplicate part??

    and then read up on this XPATH stuff...

  7. #7
    Banned indivision's Avatar
    Join Date
    Jul 2001
    Location
    Portland, OR
    Posts
    474
    attaching movieclips is very simple.

    first, right click on the clip u need to attach in the mc library and select "Linkage..."

    now, in the linkage properties panel, check 'Export for Actionscript' (and leave 'Export in first frame' selected). in the identifier field, give it the name "myClip".

    then go back to your code. to attach the movieclip to stage you would write something like this:

    Code:
    this.attachMovie("myClip","myClip1",1);

  8. #8
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    ok..Im gonna throw somethign out there..to get the ball moving..Im sure some of my syntax is WRONG (+[i] and what not)...but I'll throw it out there..to get the ball rolling somewhat..

    code:

    myXML = new XML();
    myXML.onLoad = function(success) {
    if (!success) {
    trace("XMLfailed to load");
    }else {
    //product Array
    trace("load was good");

    var productArray = [];
    productArray = myXML.products.split(",");

    for (i=0; i<productArray.length; i++) {
    trace(productArray[i]);
    //duplicate clip
    attachMovie("templateClip","productClip"+[i],[i]);
    }

    }
    };


    myXML.load("products.xml");



    Im not really sure about the split part.. I was told XML is like an ARRAY...
    but do you need to split it like that?

    basically products is the ROOT..and there are product1 , product2..etc..etc

    splitting or length from XML? same as an ARRAY from a.txt file??

    I guess lets start here..and after I get the "duplicating done..I can inquire about populating each of the DUP'd clips the reflect the DATA from each of the product nodes.


    Thanks

  9. #9
    Banned indivision's Avatar
    Join Date
    Jul 2001
    Location
    Portland, OR
    Posts
    474
    you cant, to my knowledge, directly reference the xml names like you are doing here. this is why i mentioned xpath and the xml parsers before. the parser i built makes it so you can reference by node names.

    without a parser of some kind, where you have "myXML.products" you will need something like "myXML.firstChild.childNodes" depending on your xml structure. as mentioned, its awkward to use that way.

    split turns a string into an array of the strings that are separated by a "," in the case you have. you shouldnt need to use split for anything youve described wanting to do.

    look through the XML documentation in flash to figure out how to path to the array of products via their interface.

  10. #10
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    I have looked at theXPATH link....(a nice link....little over my head..but I picked on up some of it...usage will help me understand it more)..

    ok..I didnt think the SPLIT was needed....but its just my experience so far, so I threw it out there..

    Im still a little lost..so bear with me..and forgive my thick headedness..LOL

    SO how would I use a loop to "loop" through the nodes of the "products" node?..I need to use this XPATH/parser you speak of?

    would I change:
    code:

    var productArray = [];

    productArray = myXML.products.split(",");



    for (i=0; i<productArray.length; i++) {

    trace(productArray[i]);

    //duplicate clip

    attachMovie("templateClip","productClip"+[i],[i]);

    }



    to this:
    code:

    var productArray = [];
    productArray = myXML.firstChild.childNodes

    for (i=0; i<productArray.length; i++) {

    trace(productArray[i]);

    //duplicate clip

    attachMovie("templateClip","productClip"+[i],[i]);

    }




    ok..so above...(the code)...lets say I never define an onLoad action.... so once the XML file gets loaded..it just sits there...(as I havent told FLASH what to do with it)..

    what is the format of the content then?? once loaded into the FLASHPLAYER?


    Can you get the length? Not a property of the XML object? SO would I use the nextSibling? Still unclear as to how I can loop through the childNodes of the prooducts Node...and duplicate/attach a MOVIE from the result.

    ex: for (i=0; i<myXML.firstChild.childNode.nextSibling; i++)

    ?


    thanks...

  11. #11
    Banned indivision's Avatar
    Join Date
    Jul 2001
    Location
    Portland, OR
    Posts
    474
    i believe the format flash loads xml in is basically text. the XML class parses that text. so, when you make an XML object you are making an instance of a class that can contain a loaded xml text and also has commands for parsing data out of it. the onload event is just useful for triggering actions once it has loaded an xml file.

    honestly, its been a while since ive used the xml parsing commands on the XML object so i dont recall exactly what combination of commands to use to get the array. i would have to look at the documentation. i use my parser which makes it into an easily navigable multi-dimensional object. but, i recomend you mess around with the flash parsing commands to get the array. its important that you have some understanding of that. i believe myXML.firstChild.getChildNodes() will return an array of the nodes underneath the first parent node. something like that anyway.

  12. #12

  13. #13
    Banned indivision's Avatar
    Join Date
    Jul 2001
    Location
    Portland, OR
    Posts
    474
    np. let me know how it goes or if other questions come up. if you really get stuck, i can write it out for you. but, i think you'll learn more to just read up on the XML commands.

  14. #14
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    indivision-

    sorry man, to keep bothering ya like this... dont feel obligated to respond if your busy, or whatever! your just the only one who was responding, and the XML forum is really dead..

    I think I need to take it back even a step father...

    I am just doing this:
    code:

    myXML = new XML();
    myXML.onLoad = function(){
    var products = myXML.firstChild.getChildNodes();
    //var products = myXML.firstChild.childNodes();
    //var products = this.selectNodes("/products/product1");
    trace(products);
    }
    myXML.Load("products.xml");



    (even the commented out line)

    and I get undefined.

    I obviously not trying to access it correctly..... can I not test locally with XML? (dont see why not) maybe a quick example of how to minimally load an XML file..and return...ANYTHING...??


    thanks

  15. #15
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    ok..talking/posting outloud..

    XML I am using: (exact)

    PHP Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <products>

        <product>

            <image>image1.jpg</image>

            <name>Example Shirt 1</name>

            <price>$19.99</price>

            <size>small</size>

            <description>etc..etc..etc.</description>        

        </product>



        <product>

            <image>image2.jpg</image>

            <name>Example Shirt 2</name>

            <price>$24.99</price>

            <size>medium</size>

            <description>etc..etc..etc.</description>        

        </product>

    </products>

    the simple AS I am using trying to get the total childNodes from a button load.

    code:

    on (press) {
    myXML = new XML();

    myXML.onLoad = getTotalProducts;
    function getTotalProducts(success) {
    if (success == true) {
    rootNode = myXML.firstChild;
    totalProducts = rootNode.childNodes.length;
    trace(totalProducts);
    }else {
    trace("Load Failed");
    }
    }
    myXML.load("products.xml");
    }



    now this was my FIRST test...and I am getting 0 as a result. Shouldnt I be getting 2?

    It doesnt seem to be hard.....but it wont work...LOL


    update:

    OK... Im happy I am getting a value now..
    I needed to strip this out of the xml file:

    <?xml version="1.0" encoding="ISO-8859-1"?>

    but why 5 as my result?..something to do with whitespace I assume?

    update 2:

    ok...I have been playing a little bit..and starting to get a feel...

    I have used this so far with success and understand everythign easy enough (the XML commands arent to tough)

    code:

    on (press) {
    myXML = new XML();
    myXML.ignoreWhite = true;

    myXML.onLoad = getTotalProducts;
    function getTotalProducts(success) {
    if (success == true) {
    rootNode = myXML.firstChild;
    totalProducts = rootNode.childNodes.length;
    trace("Total Products: "+totalProducts);
    trace("XML TREE:"+newline+rootNode.nodeName); //products
    trace(" "+rootNode.firstChild.nodeName); //product1
    trace(" "+rootNode.firstChild.nextSibling.nodeName); //product2
    }else {
    trace("Load Failed");
    }
    }
    myXML.load("products.xml");
    }



    I guess from here...I stop accessing the values and stuff...and try to throw a loop in there that will attach/duplicate a movieClip to match the total childNodes of my root document (products)

    I basically have a generic movieClip..that consists of several layers.

    layer1: BG fill
    layer2: an emtpy containerClip that will have an image dynamically loaded into from a VALUE in the XML tree
    layer3: dynamic textFields that will pull its data form the XML tree
    layer4: two drop down "components I guess?" (Even though i dont like the premade compoenets...and have never used the combo/drop down ever)...hopefully these choices to come from the XML tree as well...but will be the same for ALL nodes...so can possibly be harded coded for each clip (pre-duplicate)
    layer5: a button (that will eventually grab all the info in the textFields and drop downs and send off to paypal for now...or hell just take them and email...that can be the LAST step..)


    Thanks.. (I know I talk talk/post alot..but seeing it back on the screen sometimes helps me see stuff to do or was in error)

  16. #16
    Banned indivision's Avatar
    Join Date
    Jul 2001
    Location
    Portland, OR
    Posts
    474
    you had it mostly right. you just needed to add ignoreWhite=true for the xml. otherwise, the white-space confuses the flash parser.

    Code:
    myXML = new XML();
    myXML.ignoreWhite = true;
    myXML.onLoad = getTotalProducts;
    function getTotalProducts(success)
    {
    	if (success == true) {
    		rootNode = myXML.firstChild;
    		totalProducts = rootNode.childNodes.length;
    		trace(totalProducts);
    	} else {
    		trace("Load Failed");
    	}
    }
    myXML.load("test.xml");

  17. #17
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    yeah...I was updating my post..and didnt see you had responded.. (I was adding more for you to help me with!!) LOL


    knowing that I want each duplicated clip to match the values from their unique node(s).. (ie: clip1 gets: its image to load, names, title, description all from product1 node) do you think it would be better to look into using attach Clip?..or duplicate Clip?

    Have you ever used the combo box component? Is it easy to populate with XML? and target the values chosen?

    thanks

  18. #18
    Banned indivision's Avatar
    Join Date
    Jul 2001
    Location
    Portland, OR
    Posts
    474
    the components may be able to populate with xml if its structured in a certain way. i have only populated them via remoting so am not sure. the combo box probably accepts just an array of objects with their naming convention ({data:asdasd,label:asdasd}) but you'll have to check into that.

    to answer your question about how to make corresponding clips on the stage, use attachMovie. i havent used duplicateMovieClip for quite a while. i suppose there may be some circumstances that it might be convenient to use duplicate because it starts the new clip in the same position as the old, but i think its a somewhat deprecated approach because it relies on there being a clip on the stage. as you develop more with AS2 the idea in most cases is to keep the code as independent of the stage and whats on it as possible. duplicateMovieClip pre-supposes that the code knows of a clip of a certain name on the stage which may or may not be there depending on actions of other code (a section change, for example). attachMovie avoids that since you will have hard-coded an id for that object in the library.
    Last edited by indivision; 04-05-2006 at 02:20 AM.

  19. #19
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    Got ya..thanks... I will focus on using attachMovie Clip then.

    During this whole process...I had a vague idea of how to go about it.. load myXML, be abelk to target my nodes, childNodes and all values..etc..

    duplicateClip/attachClip...although I havent really used it ever...I knew that is what I needed.. the part that I honestly have no clue about is...after this is all said and done.. HOW do I populate these attachedClips with the values of their respective nodes? Im thinking it has to be done with a loop and when I am dynamically creating them.... correct?

  20. #20
    Banned indivision's Avatar
    Join Date
    Jul 2001
    Location
    Portland, OR
    Posts
    474
    attachMovie returns a reference to the clip that was created. so you would create a for ... in loop on your list of nodes, each iteration running attachMovie and saving the reference to the new clip as a var. then transport the variables from the xml (using nodeValue i believe) into the var reference.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center