7 Replies Latest reply on May 4, 2010 3:13 PM by Cancerinform

    Loading XML Data

    beno12345 Level 1

      Hi;

      I have the following code:

       

              var pageDetails:XML;
              /*
              var pageDetails:XML = <PAGE name='index'>
                <DETAILS>This is the home page.</DETAILS>
              </PAGE>;
              */
              var urlXMLLoader:URLLoader;

       

              public function DeltaMain()
              {
                  XMLLoader();
              }
             
              function XMLLoader()
              {
                  var urlXMLRequest:URLRequest = new URLRequest('pageDetails.xml');
                  urlXMLLoader = new URLLoader();
                  urlXMLLoader.addEventListener(Event.COMPLETE, completeXMLListener);
                  urlXMLLoader.load(urlXMLRequest);
              }
             
              function completeXMLListener(e:Event):void
              {
                  pageDetails = new XML(urlXMLLoader.data);
              }

       

      In another function I have the following:

       

              myText.htmlText = pageDetails.text;

      Now, if I uncomment the above (and pull out the previous definition of pageDetails), everything works just fine. However, if I leave the comment, it throws the following error:

       

      TypeError: Error #1009: Cannot access a property or method of a null object reference.
          at DeltaMain/Text()
          at DeltaMain()

       

      Now, the file pageDetails.xml does exist in the appropriate folder with the following, essentially identical to the above comment:

       

      <PAGE name='index'>
        <DETAILS>This is the home page.</DETAILS>
      </PAGE>


      Please advise.

      TIA,

      beno

        • 1. Re: Loading XML Data
          Cancerinform

          The reason why you get the error is that the textfield text is not available until the xml file is loaded, however you want to display the text before that. You need to either add the textfield script inside the completeXMLListener function or add an event dispatcher inseide of that function.

          • 2. Re: Loading XML Data
            beno12345 Level 1

            Ok, that's closer, but I still don't have it. I changed to this:

             

                    function completeXMLListener(e:Event):void
                    {
                        pageDetails = new XML(urlXMLLoader.data);
                        pageDetails = pageDetails.text
                    }

             

             

            and got this:

             

            TypeError: Error #2007: Parameter text must be non-null.
                at flash.text::TextField/set htmlText()
                at DeltaMain/Text()
                at DeltaMain()
            TypeError: Error #1034: Type Coercion failed: cannot convert XMLList@217621c9 to XML.
                at DeltaMain/completeXMLListener()
                at flash.events::EventDispatcher/dispatchEventFunction()
                at flash.events::EventDispatcher/dispatchEvent()
                at flash.net::URLLoader/onComplete()

             

            So it didn't like the way I tried to convert. Can you be specific how to script this?

            TIA,

            beno

            • 3. Re: Loading XML Data
              Cancerinform Level 2

              First of all, do not give the same identifiers to all variables. If myText is the identifier of the textfield then to get to the node you need to write:

               

              var xmlData:XML = XML (event.target.data);
              var pageDetails:String = xmlData.DETAILS.toString();
              myText.text = pageDetails; //This is the home page.

              • 4. Re: Loading XML Data
                beno12345 Level 1

                Great. Almost there. The pageDetails var traces out the necessary; however, I had to change your line to this:

                 

                            myText.htmlText = pageDetails;

                 

                because I'm += the myText var with htmlText, and in fact will substitute the pageDetails material with HTML text. When I run a trace after the above line, I get:

                 

                [object TextField]

                 

                which, of course, is what I want. However, it prints nothing to the resultant swf page, so I'm obviously missing something. Please advise.

                TIA,

                beno

                • 5. Re: Loading XML Data
                  Cancerinform Level 2

                  I am not familiar with your whole movie. So I don't know what you want to achieve in the end.

                  • 6. Re: Loading XML Data
                    beno12345 Level 1

                    I took this to another list after not hearing back for a day. The problem was in calling Text(), which called the text variable, before actualizing the same. Have the credit anyways, since you helped a lot

                    beno

                    • 7. Re: Loading XML Data
                      Cancerinform Level 2

                      I will add that to my credit report