Skip to main content
spvr
Inspiring
March 12, 2010
Answered

How to import XML data?

  • March 12, 2010
  • 2 replies
  • 1424 views

Please explain with examples

regards

spvr

This topic has been closed for replies.
Correct answer Harry Kunz

For example you have an XML file of name "myxml.xml" inside the same directory of your fla with the following contents:

<?xml version="1.0" encoding="UTF-8"?>

<myrootnode>

   <mychildnode id="1">Something111</mychildnode>

   <mychildnode id="2">Something222</mychildnode>

</myrootnode>

//
//You can load it through the URLLoader class:

var cXmlLoader:URLLoader = new URLLoader();

var cUrlReq:URLRequest = new URLRequest("myxml.xml");

cXmlLoader.addEventListener(Event.COMPLETE, onXmlLoad);

cXmlLoader.load(cUrlReq);

function onXmlLoad(oEvent:Object):void

{

   XML.ignoreWhitespace = true;

   var cXmlData:XML = new XML(oEvent.target.data);

   trace(cXmlData.mychildnode[0]);

   trace(cXmlData.mychildnode[1]);

}

If you had more child nodes under each of the child nodes then you could access them by simple dot notation using their node names.

2 replies

Participant
March 30, 2011

you can try this example.it uses URLLoader in flex to extract XML data

http://flexparsexml.blogspot.com/

Harry KunzCorrect answer
Inspiring
March 12, 2010

For example you have an XML file of name "myxml.xml" inside the same directory of your fla with the following contents:

<?xml version="1.0" encoding="UTF-8"?>

<myrootnode>

   <mychildnode id="1">Something111</mychildnode>

   <mychildnode id="2">Something222</mychildnode>

</myrootnode>

//
//You can load it through the URLLoader class:

var cXmlLoader:URLLoader = new URLLoader();

var cUrlReq:URLRequest = new URLRequest("myxml.xml");

cXmlLoader.addEventListener(Event.COMPLETE, onXmlLoad);

cXmlLoader.load(cUrlReq);

function onXmlLoad(oEvent:Object):void

{

   XML.ignoreWhitespace = true;

   var cXmlData:XML = new XML(oEvent.target.data);

   trace(cXmlData.mychildnode[0]);

   trace(cXmlData.mychildnode[1]);

}

If you had more child nodes under each of the child nodes then you could access them by simple dot notation using their node names.

Inspiring
March 12, 2010

Adobe Forum (but i think YOU) wrote:

thanks harry.
i have a another problem with xml data load if i use class.
please check following code

package {
    import flash.xml.*;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.errors.IOError;
    import flash.events.IOErrorEvent;
    import flash.display.MovieClip;
   
    public class xmldata {

        public var loader:URLLoader = new URLLoader();
   
        public function xmldata() {
            loader_fun();

        }
       
        private function loader_fun(){
        loader.addEventListener(Event.COMPLETE, onLoadXML);
            loader.load(new URLRequest("carData.xml"));
        }
           
            public function onLoadXML(ev:Event) {
            try {
               
                var myXML:XML = new XML(ev.target.data);
                trace(myXML);
            } catch (e:TypeError) {
               
                trace("Could not parse the XML");
                trace(e.message);
            }
        }
    }
}

Hey, something weird just happened. I received your reply but instead of your display name, it was "Adobe Forums <forums@adobe.com>", that's weird. Anyway. I don't see any problem with your code. It works fine in my setup. Just make sure your file has the same name as the class name "dataxml" and it is in the same directory as your fla and use this code in the fla:
import xmldata;
var cXmlData:xmldata = new xmldata();
I receive the trace of the xml content. Are there any errors you received?