Skip to main content
Known Participant
November 29, 2009
Question

Cannot load XML

  • November 29, 2009
  • 1 reply
  • 488 views

I dont know much about Parsing XML with AS2. Any idea why this doesnt work? "Game can now be started", "In loadXML function" is traced. Unfortunately, "failed to load" is traced too. I put my cards.xml file in the same file as my main.asc.

===============================================================

Server side (main.asc)

===============================================================

application.onConnect = function(clientObj){
    application.acceptConnection(clientObj);
    clientObj._uniqueUserID = this.userID;
    this.userID++;
   
    if(this.userID == 2)
    {
        trace("Game can now be started");
        for (i = 0; i < application.clients.length; i++){
            application.clients.call("startGame");
        }
    }
   
    clientObj.loadXML= function(){
            var cardData = new XML();
            cardData.load("http://localhost/myapplication2/cards.xml");
            trace("In loadXML function");
            cardData.ignoreWhite = true;
            cardData.onLoad = function(success){
                if(success)
                {
                    trace("xml loaded");
                }
                else
                {
                    trace("failed to load");
                }
            }
        }
}

====================================================

Client side

====================================================

function startGame():void
{
    nc.call ("loadXML", null);

}

    This topic has been closed for replies.

    1 reply

    calmchessplayer
    Inspiring
    November 29, 2009

    This ("http://localhost/myapplication2/cards.xml");
                 is an address on a webserver where as the application folder containing the main.asc is local host...you either need to put the xml on a webserver or try to load the xml file using this ("cards.xml");


    Known Participant
    November 30, 2009

    you're right, I have to upload the xml into some webserver and get that url, but I can't seem to test it locally, cardData.load("cards.xml"); doesn't seem to work either, even if I copy the whole directory ("C:/User/.........cards.xml");

    Any ideas how to test it locally?

    December 1, 2009

    If you want to read an XML file in the application folder, use the File() class to read the contents of the XML file into a string, and then use the XML() class to parse that string into XML.

    Also, if the XML data is the same for all clients, you'd be better off just loading it once in your onAppStart handler, rather than loading it each time a client calls for it.