A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Lost in XML

  1. #1
    Newbie Designer
    Join Date
    May 2005
    Posts
    42

    Lost in XML

    Hi all,

    I have been trying to work out how to get my actionscript to work with my XML but it is just beyond me. My XML is simple but long, sample below.
    Code:
    <category01>
     
    <project01>
        <project01title></project01title>
        <project01description>project01description>
    </project01>
     
    <project02>
        <project02title></project02title>
        <project02description></project02description>
    </project02>
     
    </category01>
    There are 15 categories and 30 projects in each category controlled through script in movie clips for each of the 15 categories. When the user clicks on a category on the left, the category thumbnail image set (30 buttons) pop up, then the user clicks a thumbnail, the categories project container goes to a point on the timeline to play that projects image loading script. Example below of what I have in there for project01.

    Code:
    //Load thumbnails and first of the large images into movieclip containers
    loadMovie("resources/projects/default/thumbnail/project01_TN1.jpg", "project01_TN1");
    loadMovie("resources/projects/default/thumbnail/project01_TN2.jpg", "project01_TN2");
    loadMovie("resources/projects/default/thumbnail/project01_TN3.jpg", "project01_TN3");
    loadMovie("resources/projects/default/large/project01_LG1.jpg", "project01_LG"); 
    //Load XML
    var project01_xml = new XML();
    project01_xml.ignoreWhite = true;
    project01_xml.contentType = "text/xml";
    project01_xml.onLoad = function(success) { 
       if (success) {
           //target_dynamic_text_box.text = xml_node.nodeValue;
           //Puts the title in the title box and description in the description box
           project01title.text = this.firstChild.firstChild.firstChild.firstChild.nodeValue;
           project01description.text = this.firstChild.firstChild.firstChild.nextSibling.firstChild.nodeValue;
       }  else {
           project01title.text = "Error";
           project01description.text = "Error";
       }
    };
    project01_xml.load('resources/projects/xml/projectinfo.xml');
    stop();
    As you can see I just have a bunch of firstChild and nextSiblings if I keep going this way. All I want to be able to do is say firstChild * incremental-number for example. I dont understand how to implement a loop to sort this out for me. Any help greatly appreciated.

  2. #2
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    Im a little lost in what you are trying to do?..or get at?

    maybe some better nesting and some attributes in your XML and dumping to a named/associative array might help things be cleaner/easier??

    you are already nesting all yo project nodes/data under 1 parent node/element..

    you should do the same for the categories..

    how or where are you implementing how you click on a thumbnail and it jumps to a certain frame inside a specific clip? (I dont see that posted)

    are you using frame labels or vars to hold frame numbers or something?

    anyways.. here is a quick demo or loading your XML and parsing it into a multi-dimensional associative array.

    FLASH CS3 (AS2.0)

    XML:
    PHP Code:
    <categories>

        <
    category01 name="Category 1">
     
            <
    project01 name="">
                    <
    project01title>1-1 title</project01title>
                    <
    project01description></project01description>
            </
    project01>
     
            <
    project02 name="">
                    <
    project02title>1-2 title</project02title>
                    <
    project02description></project02description>
            </
    project02>
     
        </
    category01>

        <
    category02 name="Category 2">
     
            <
    project01 name="">
                    <
    project01title>2-1 title</project01title>
                    <
    project01description></project01description>
            </
    project01>
     
            <
    project02 name="">
                    <
    project02title>2-2 title</project02title>
                    <
    project02description></project02description>
            </
    project02>
     
        </
    category02>

        <
    category03 name="Category 3">
     
            <
    project01 name="">
                    <
    project01title>3-1 title</project01title>
                    <
    project01description></project01description>
            </
    project01>
     
            <
    project02 name="">
                    <
    project02title>3-2 title</project02title>
                    <
    project02description></project02description>
            </
    project02>
     
            <
    project03 name="">
                    <
    project02title>3-3 title</project02title>
                    <
    project02description></project02description>
            </
    project03>
     
        </
    category03>


    </
    categories

    actionscript Code:
    //array for all data
    var dataArray: Array = new Array();
    //Load XML
    var allData_xml = new XML();
    allData_xml.ignoreWhite = true;
    allData_xml.contentType = "text/xml";
    allData_xml.onLoad = function(success) {
        if (!success) {
            //failed to load XML
            trace("FAILED TO LOAD XML FILE");
        } else {
            trace("LOADING XML FILE....BE PATIENT");
            var totalCategories:Number = allData_xml.firstChild.childNodes.length;
            trace("TOTAL CATEGORIES: "+totalCategories);
            for(i=0; i<totalCategories; i++){
                var categoryName:String = allData_xml.firstChild.childNodes[i].attributes.name;
                //store this data into our MAIN array
                dataArray.push({catNum:i, category:categoryName, projects:new Array()});
                //get total projects for each category
                trace("CATEGORY NAME: "+categoryName);
                var totalProjects:Number = allData_xml.firstChild.childNodes[i].childNodes.length;
                trace("   -TOTAL PROJECTS: "+totalProjects);
                for(e=0; e<totalProjects; e++){
                    var projectTitle = allData_xml.firstChild.childNodes[i].childNodes[e].childNodes[0].firstChild.nodeValue;
                    var projectDescription = allData_xml.firstChild.childNodes[i].childNodes[e].childNodes[1].firstChild.nodeValue;
                    trace("     PROJECT NAME: "+projectTitle);
                    dataArray[i].projects.push({projNum:e, project:projectTitle, description:projectDescription});
                }
            }
        }
    };
    allData_xml.load('data.xml');




    you can then use this code to access your array form anywhere and build or access any data based on it..

    I can take this array and do the following now:

    1.) build my CATEGORY menu..
    (and assign any/all projects (data) to this menu button..so when clicked..it loads all thumbnails/project associated with that category)

    2.) when loading/attaching all thumbnails to the stage I can assign actions so that whe clicked.. it can jump to a certain frame in any clip or movie on he stage..

    again I am not really following your project needs or the code posted.. but maye you can tweak this to your requirements.


    Using the array form my example above..

    MAIN ARRAY (dataArray)

    dataArray[]

    to access a category you put in the number

    dataArray[0] is for category 1

    dataArray[1] is for category 2...etc


    each category has 3 properties you can access

    catNum

    category (which is the category name)

    projects (which is an array of all projects associated with that category)


    to access what category this is, I can do:
    dataArray[0].catNum // outputs 0


    to access the category name I am in. I do:
    dataArray[0].category; //outputs category name for category 0/first category


    to find out how many projects are associated with this category.. I do:
    dataArray[0].projects.length;

    if I wanted to see how many projects are in category 3.. I do:
    dataArray[2].projects.length;



    now.. each project ALSO has a group of properties you can access/use in your movie..

    projNum (project number it is)

    project (project title)

    description (desc. of project)

    to access them.. I need to decide on what category I want first

    dataArray[0] (zero for example is the first category)


    then I need to access that categories projectArray property...and pick a project I wanted to grab details from.. (we'll do project number 2)


    dataArray[0].projects[1]. <--- PROPERTY NAME GOES HERE...

    so to get the description of my project I would do:

    trace("DESCRIPTION: "+dataArray[0].projects[1]. description);


    hope this helps..

  3. #3
    Newbie Designer
    Join Date
    May 2005
    Posts
    42

    Amazing

    Man... that was an epic post but broke it down in such understandable terms i think it just might work for me.

    I haven't implemented it yet but it sounds exactly like what I was after because it basically allows me to code in almost actual sentences rather than jargon.

    Thanks a lot mate.

  4. #4
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    yeah it was long..but I tried to make it short and direct (to the point) sentences... lol

    once you get used to it.. you can shorten things up..

    I also suggest using a movieClipLoader instance..instead of just loadMovie();

    you can alot more 'control'..

    in my footer is a link (clipLoader).. if you want to see how it works..(its easy once you use it a few times)..

    actually there are alot of nice links in my footer... (such as attachMovie...etc)
    that may benefit you and your current project...

    take care. =)

  5. #5
    Newbie Designer
    Join Date
    May 2005
    Posts
    42

    Combine with Line Breaks

    Hey,
    Is there a way to combine these properties into a dynamic text box, complete with line breaks between each value?
    For example, One text box with the project title, description, and location (added since getting your last response).
    Actionscript Code:
    projectdecription.text = _parent.projectArray[0].projects[0].project
    +  _parent.projectArray[0].projects[0].description
    + _parent.projectArray[0].projects[0].projectlocation;
    The above doesn't work... just an example of what I would have thought would work.

  6. #6
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    it depends on where the text field is in relation to the 'code'

    projectdecription.text = _parent.projectArray[0].projects[0].project
    +newline+ _parent.projectArray[0].projects[0].description
    +newline+ _parent.projectArray[0].projects[0].projectlocation;

  7. #7
    Newbie Designer
    Join Date
    May 2005
    Posts
    42
    The whole _parent thing must be correct because I can get them all to load into separate boxes fine, its just when I try to join them in the one text box that i have a problem... +newline+ didn't seem to work... is there something I have to do before that?

  8. #8

  9. #9
    Newbie Designer
    Join Date
    May 2005
    Posts
    42
    My apologies... turns out I had the most rookie error...

    I typo'd my instance name, never thought to check that. The +newline+ method worked a treat. thanks again.

    Next question though... turns out if I am to join them I need to change font for one of the attributes (chinese translation of the description)... is it possible to implement TextFormat() halfway through my joining attributes?

    example of my thinking but dont know the actual way to do it:

    projectdescription.text = [font=Arial(_parent.projectArray[0].projects[0].project)]
    +newline+ [font=Arial(_parent.projectArray[0].projects[0].description]
    +newline+ [font=SimSun(_parent.projectArray[0].projects[0].descriptionChinese)];
    Last edited by BangusBoy; 04-15-2011 at 12:54 AM. Reason: addition of extra text

  10. #10
    Newbie Designer
    Join Date
    May 2005
    Posts
    42
    Using your code (modified) from above, is there a way to say - if a value is undefined textbox="" - rather than it showing up with the word undefined? At the moment I have some xml tags that arent populated with any info yet and would rather it show up with "Insert text here" or something similar or even blank.

    Actionscript Code:
    //array for all data
    var projectArray: Array = new Array();
    //Load XML
    var projectData_xml = new XML();
    projectData_xml.ignoreWhite = true;
    projectData_xml.contentType = "text/xml";
    projectData_xml.onLoad = function(success) {
        if (!success) {
            //failed to load XML
            trace("FAILED TO LOAD XML FILE");
            } else {
                //trace("LOADING XML FILE....BE PATIENT");
                var totalCategories:Number = projectData_xml.firstChild.childNodes.length;
                //trace("TOTAL CATEGORIES: "+totalCategories);
                for(i=0; i<totalCategories; i++){
                    var categoryName:String = projectData_xml.firstChild.childNodes[i].attributes.name;
                    //store this data into our MAIN array
                    projectArray.push({catNum:i, category:categoryName, projects:new Array()});
                    //get total projects for each category
                    //trace("CATEGORY NAME: "+categoryName);
                    var totalProjects:Number = projectData_xml.firstChild.childNodes[i].childNodes.length;
                    //trace("   -TOTAL PROJECTS: "+totalProjects);
                    for(e=0; e<totalProjects; e++){
                        var projectTitle = projectData_xml.firstChild.childNodes[i].childNodes[e].childNodes[0].firstChild.nodeValue;
                        var projectDescription = projectData_xml.firstChild.childNodes[i].childNodes[e].childNodes[1].firstChild.nodeValue;
                        var projectDescriptionCHN = projectData_xml.firstChild.childNodes[i].childNodes[e].childNodes[2].firstChild.nodeValue;
                        var projectLocation = projectData_xml.firstChild.childNodes[i].childNodes[e].childNodes[3].firstChild.nodeValue;
                        var projectfact1 = projectData_xml.firstChild.childNodes[i].childNodes[e].childNodes[4].firstChild.nodeValue;
                        var projectfact2 = projectData_xml.firstChild.childNodes[i].childNodes[e].childNodes[5].firstChild.nodeValue;
                        var projectfact3 = projectData_xml.firstChild.childNodes[i].childNodes[e].childNodes[6].firstChild.nodeValue;
                        //trace("     PROJECT NAME: "+projectTitle);
                        projectArray[i].projects.push({projNum:e, project:projectTitle, descriptionAUS:projectDescription, descriptionCHN:projectDescriptionCHN, projectLocation:projectLocation, fact1:projectfact1, fact2:projectfact2, fact3:projectfact3});
                    }
                }
            }
        };
    projectData_xml.load('resources/projects/xml/projectinfo.xml');

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