6 Replies Latest reply on Apr 25, 2007 12:09 AM by Blue_Pixel

    XML Load

    Blue_Pixel
      Hello

      I'm trying to load an XML file into my project. I've tried to use:
      var myXML:XML = new x();
      or
      var myXML:XMLDocument = new XMLDocument();
      but in those cases I don't have a method " Load( "path_to_xml" ) " to load the external XML.
      Please tell me how can I do this?
      Thank you,
      Tudor
        • 1. Re: XML Load
          chris.huston.t10 Level 3
          Here is some code you can use to load an external xml file:

          private var xmlVariable:XML = new XML();

          private function loadXML():void {
          var myLoader:URLLoader;
          var XML_URL:String = " http://www.yourpath.com/filename.xml";
          var myXMLURL:URLRequest = new URLRequest(XML_URL);
          myLoader = new URLLoader(myXMLURL);
          myLoader.addEventListener("complete", xmlLoaded);
          myLoader.addEventListener("ioError", xmlError);
          myLoader.addEventListener("securityError", xmlError);
          }

          private function xmlError(evt:Event):void {
          Alert.show(evt.text);
          }

          private function xmlLoaded(evt:Event):void {
          xmlVariable = XML(evt.currentTarget.data);
          }
          • 2. Re: XML Load
            Blue_Pixel Level 1
            Masamune, thank you for suggestion but I have one more problem: after I've included that code into an external file (myScript.as) witch I imported into my project:

            <mx:Script source="as/myScript.as" >
            </mx:Script>

            But how can I call that method? even if I declare private var xmlVariable:XML = new XML() in myScript.as, I can't use that method for it. I've also tried to include in into project:

            <mx:Script>
            <![CDATA[
            include "as/myScript.as"
            var xm:XML = new XML();
            xm.loadXML(); <--- this won't work
            ]]>
            </mx:Script>
            So, how can I call the method defined that way and effectively use it for loading the XML.
            • 3. Re: XML Load
              chris.huston.t10 Level 3
              Just call the function directly after including it as with any other function like this:

              <mx:Script>
              <![CDATA[
              include "as/myScript.as"
              var xm:XML = new XML(); --this is not needed unless you want to use this xm variable for something else.
              loadXML();
              ]]>
              </mx:Script>

              Vygo
              • 4. Re: XML Load
                Blue_Pixel Level 1
                I understand what you've wrote and thank your for your help. But here's another problem:
                public function loadXML():void
                {
                var myLoader:URLLoader;
                var XML_URL:String = "language/language.xml";
                var myXMLURL:URLRequest = new URLRequest(XML_URL);
                myLoader = new URLLoader(myXMLURL);
                myLoader.addEventListener("complete", xmlLoaded);
                myLoader.addEventListener("ioError", xmlError);
                myLoader.addEventListener("securityError", xmlError);
                Alert.show (myLoader.data); <--- this don't display anything
                }
                I've tried also Alert.show(myLoader.bytesTotal.toString()), it displays 0. the file is correct, I have a directory "language" and the xml it's called "language.xml".
                I've called the method this way:
                <mx:Button x="200" y="200" label="Button" mouseDown="loadXML()" />
                and the allert message is like I said... I want to actually be able to get some access some nodes from that xml and to use them. Sorry if I don't understand, but I can't manage to solve this problem.
                Tudor
                • 5. Re: XML Load
                  chris.huston.t10 Level 3
                  All Flex functions are asynchronous which means that when you use a function to load an external data source, you need to listen for the result with a different function. Your alert call is in the same function that is loading the XML so it will always be 0 since the XML has not been loaded yet. To access your XML, put your alert (or you can use a trace) in the xmlLoaded function:

                  private function xmlLoaded(evt:Event):void {
                  xmlVariable = XML(evt.currentTarget.data);
                  alert(evt.currentTarget.data);
                  trace(evt.currentTarget.data);
                  }

                  Vygo
                  • 6. Re: XML Load
                    Blue_Pixel Level 1
                    Thank you very much.