0

Background

I have a computer based training project that I need to build a standardized wrapper for. The pages for the course will be created using XML. Each page will have it's own XML file; Example, Page 1 will load all the content from 1.xml and Page 2 will be 2.xml and so-forth. The amount of pages will always be different since not all clients are the same.

Problem

I have created ten XML files to test with, but for the life of me I cannot figure out the following:

  • using Flash, count the amount of xml files in the folder (I do not have access to any server-side, if I did that would change everything) I thought about using IOErrorEvent.IO_ERROR with some type of loop, but I didn't know where to even begin
  • using Flash, load all the information from all the xml files into one Array or Vector to display on the page as the user progresses thru the course. The user may skip thru the course, example, they may want to skip from page 5 to 20 or what ever. So an object or associative array was my idea

I understand Flash cannot access the File system, but since the xml files will be named 1.xml,2.xml,3.xml,4.xml,5.xml, etc... (this naming convention will never change since the XML file will be directly related to the page number) there should be a way to do this. My idea was to create some type of loop that will try and load each file. Once an error is thrown due to a file not found I will break out of the loop. For a visual look at my working files below.

Current Working Files

The files I've created and started to work with has little information since I don't even know how to approach this. But, here's what I have:

xml files (Remember, there could be 5 or 100 or whatever else the client needs, but they all will be formatted the same)(Also, I DO NOT have access to any type of database so XML files seem to be my best option)

<?xml version="1.0" encoding="iso-8859-1"?>
<content>
    <title>My First Page</title>
    <body>This is the body of my new page I just built using XML</body>
    <img>graphic.jpg</img>
</content>

XmlClass.as ** SEE BELOW FOR CODE, Iv'e updated it **

Like I said, this is a starting point. A ton of things need to be worked out, but I want to make sure the road I am headed down is the right one ;) I am not sure if this xml idea is even the best way to approach this. I just thought if the client creates and maintains the xml files on their own, it would take some of the workload off of me.

Thanks!

EDIT

Judging from my responses my question was poorly written. Here is the XmlClass.as file I have been working on with comments that may explain better than I did above. You can see in the code that I create a method that builds a _loaderArray that I can use in a for loop to Load each link to the xml files.

All I am trying to do is take x amount xml files and create an array or something that will allow me to have all the xml files on one place.

XmlClass.as

package
{
import flash.display.*;
import flash.xml.*;
import flash.events.*;
import flash.net.*;

public class XmlClass extends XMLDocument
{       
    private var _xml:XML;       
    private var _loader:URLLoader;
    private var _request:URLRequest;
    private var _loaderArray:Array; /* this is to store my paths to each xml file */
    private var _xmlArray:Array; /* this is where I want to store all the information in all the xml files */
    private var _fileError:Boolean; /* this tells me that I have hit a 404 trying to load one of the files */

    public function XmlClass():void
    {
        BuildLoaderArray();
        BuildXmlArray();
    }
    /* build the xml array so I can have one place that stores each of the xml files content */
    private function BuildXmlArray():void
    {
        _loader  = new URLLoader();
        for(var i=1;i<_loaderArray.length;i++)
        {
            /* check to see if we have a 404 error and stop the loop */
            if(_fileError){break;}
            _request = new URLRequest(_loaderArray[i]);/* load each of the xml paths to load into the array */
            _loader.addEventListener(Event.COMPLETE,ProcessXML);
            _loader.addEventListener(IOErrorEvent.IO_ERROR,XMLioErrorHandler); 
            _loader.load(_request);
        }
        trace(_xmlArray);/* show me the newly created xml array that has all the files information */
    }
    /* build all the links to the xml files for the loader */
    private function BuildLoaderArray():void
    {
        _loaderArray = new Array();
        for(var i=1;i<201;i++)
        {
            _loaderArray.push("../xml/"+i+".xml");
        }
    }
    private function ProcessXML(e:Event):void
    {
        _xmlArray = new Array();
        _xml = new XML(e.target.data);
        _xmlArray.push(_xml);
    }       
    private function XMLioErrorHandler(e:IOErrorEvent):void
    {
        _fileError = true;
        trace("Loading Error")
    }
}
}   
9
  • make an xml to configure your project. For example it will have nodes that have the information in it you will need the page numbers and url to the corresponding xml for that page. This way the project can be configured dynamically right from an xml.
    – The_asMan
    Apr 19, 2012 at 16:57
  • @ The_asMan : I like that idea and did see something similar on the web, but the idea I had in my head was to make this dynamic as possible with little interaction from me. There has to be a way to do this with out a config file. If I cannot find a solution then your idea will be my only option.
    – Mike
    Apr 19, 2012 at 17:02
  • since you do not have access to the server side scripts then there is no way at all you can generate dynamic material.
    – The_asMan
    Apr 19, 2012 at 18:02
  • I agree with what your saying .. to a degree. To do it properly, yes I would need server-side. But, I do believe there is a work-around to this problem.
    – Mike
    Apr 19, 2012 at 19:03
  • Well then your only other option would be to try to load them in order IE: 1.xml,2.xml,3.xml... and keep going until the server responds with a 404. Just like you have
    – The_asMan
    Apr 19, 2012 at 19:25

1 Answer 1

0

this is not proper way to manage the multiple xml file to be loaded. first of all you have to make one configure xml file which will contain the list of xml files which will be loaded as part of your data and you need to load first configure xml file and then after you have to start content xml files as per the configure xml file.

you can get flexibility to configure your xml file name and count also. it will work any case and no need to give any thing run-time. whenever you/ your client want to change, just change the configuration xml file. it's done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.