9 Replies Latest reply on Jun 20, 2007 1:29 PM by ntsiii

    Load XML and edit

    XMLmaster
      Hi I am new to Flex and AS3 but I have some experience with XML, XSLT, XPATH, VB etc..

      I am having trouble just loading the XML. How do I load an external XML file so I can edit it?

      This is what I have so far:

      <mx:Script>
      <![CDATA[
      var myxml = 'xml/request.xml';
      XML.ignoreWhitespace = true;
      var xml:XML = new(myxml);
      ]]>
      </mx:Script>

      I recieve an error:

      "TypeError: Error #1007: Instantiation attempted on a non-constructor."

      Thanks in advance
        • 1. Re: Load XML and edit
          buabco Level 1
          HI!

          You have a lot of stuff missing :)

          FIrst, the XML Object does not load stuff by itself like that. You need the URL loader for this

          try:

          var rq:URLRequest = new URLRequest('xml/request.xml');
          var l:URLLoader = new URLLoader(rq);

          This will load the XML into your flash movie, even though you'll have no XML until it's loaded, so you need to add an Event listener for this:

          l.addEventListener(Event.COMPLETE, finishLoading);

          Now you need to implement the finishLoading function:

          public function finishLoading(evt:Event):void {
          var myXML:XML = new XML(evt.target.data);
          }

          And there you have it :)




          • 2. Load XML and edit
            XMLmaster Level 1
            Thanks for the help. I am a little lost when it come to as3 and FLEX. :)

            My code so far is:

            <mx:Script>
            <![CDATA[
            var rq:URLRequest = new URLRequest('xml/request.xml');
            var l:URLLoader = new URLLoader(rq);
            l.addEventListener(Event.COMPLETE, finishLoading);


            public function finishLoading(evt:Event):void {
            var myXML:XML = new XML(evt.target.data);
            }
            ]]>
            </mx:Script>

            But I recieve these two error...

            1120: Access of undefined property finishLoading.
            1120: Access of undefined property l.
            • 3. Re: Load XML and edit
              buabco Level 1
              what is the container for the script tag? is it an application object?
              • 4. Re: Load XML and edit
                XMLmaster Level 1
                I am using FLEX 2.

                So the Script Tag identifies the content is AS3 not MXML. Any ideas on how to load and edit?



                • 5. Re: Load XML and edit
                  buabco Level 1
                  hmmm I'm kind of confused, if you are not using MXML then there is no place for the <mx:SCRIPT> tag, you should create a AS3 class that has the code you just showed me, with no tags anywhere.
                  if you are using MXML your code should be something like:

                  <mx:Application .... (Some more stuff here>
                  <mx:Script>
                  ..... the code ....
                  </mx:Script>
                  </mx:Application>

                  If you are just using the Script tag you have a problem :)
                  • 6. Re: Load XML and edit
                    Level 7
                    use

                    mx:Script>
                    > <![CDATA[
                    > var myxml = 'xml/request.xml';
                    > XML.ignoreWhitespace = true;
                    > var xmlobj:XML = new XML;
                    xmlobj.load("myxml ");
                    > ]]>
                    > </mx:Script>



                    "XMLmaster" <webforumsuser@macromedia.com> wrote in message
                    news:f273l6$ppb$1@forums.macromedia.com...
                    > Hi I am new to Flex and AS3 but I have some experience with XML, XSLT,
                    > XPATH,
                    > VB etc..
                    >
                    > I am having trouble just loading the XML. How do I load an external XML
                    > file
                    > so I can edit it?
                    >
                    > This is what I have so far:
                    >
                    > <mx:Script>
                    > <![CDATA[
                    > var myxml = 'xml/request.xml';
                    > XML.ignoreWhitespace = true;
                    > var xml:XML = new(myxml);
                    > ]]>
                    > </mx:Script>
                    >
                    > I recieve an error:
                    >
                    > "TypeError: Error #1007: Instantiation attempted on a non-constructor."
                    >
                    > Thanks in advance
                    >


                    • 7. Re: Load XML and edit
                      xrum
                      hi, i'm having the same issue
                      my code is in the MXML file, in the "Script" tag.

                      i included flash.net.*;
                      and keep getting those 4 errors. please help :(
                      • 8. Re: Load XML and edit
                        Teuzze
                        I did that in my application for loading a XML file. I hope it helps you :)


                        <?xml version="1.0" encoding="utf-8"?>
                        <mx:Application
                        xmlns:mx=" http://www.adobe.com/2006/mxml"
                        xmlns:custom="*"
                        layout="absolute"
                        horizontalScrollPolicy="off"
                        backgroundAlpha="0"
                        creationComplete="init();"
                        >
                        <mx:Script>
                        <![CDATA[
                        import mx.controls.Image;
                        import flash.display.*;
                        import flash.events.IOErrorEvent;
                        import flash.net.URLLoader;
                        import flash.net.URLRequest;
                        import flash.events.Event;
                        import flash.events.ProgressEvent;

                        public function init():void
                        {
                        var loaderSkin:URLLoader = new URLLoader();
                        var requestSkin:URLRequest = new URLRequest("XML/skin.xml");
                        loaderSkin.load(requestSkin);
                        loaderSkin.addEventListener(Event.COMPLETE, finDuChargementSkin)
                        }

                        public function finDuChargementSkin ( event:Event ):void {
                        var skin:XML = new XML (event.target.data)
                        \\put your code here
                        }
                        ]]>
                        </mx:Script>
                        <mx:Label id="myLabel" width="1000" fontWeight="bold" fontSize="12"/>
                        </mx:Application>
                        • 9. Re: Load XML and edit
                          ntsiii Level 3
                          The key is that you cannot do complex assignments outside of a function because of the way the msml is generated into AS then compiled into the swf. The declared objects do not exist yet.

                          Declare the variable in the instance scope, but, as in Teuzze's example call an init function from the initialize or creationComplete event, and do the work in there.

                          Tracy