3 Replies Latest reply on Sep 11, 2009 8:11 AM by babo_ya

    Reading xml file

    Bifz

      Hello everyone,

       

      I am sorry for putting one more post on reading xml file in flex, but I think I need more explanation. So, I am building a web application using flex, in which I need to read data from a xml file. The file will be located in the same forlder as my application. Thus, I have to read data from one of the nodes and display it in a <Text> component. After reading all older posts, I could successfully read xml data in the the text component, but only if xml data is present as XMLList.

       

      What I need to do is load the whole xml in my webapp and then read one particular node of the xmlfile.

       

      I could successfull do this till now,


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

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="800" height="570" layout="absolute" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[0xFFFFFF,0xFFFFFF]" initialize="initializeHandler();">

         
      <mx:Script>
              <![CDATA[
             
                  [Bindable] public var a:XMLList;
         
                  [Bindable]
                  private var myBooks:XML =
                      <books>
                          <book>
                              <title>Foundation ActionScript Animation: Making Things Move</title>
                              <author>Keith Peters</author>
                          </book>
                      </books>
                 
                  private function initializeHandler():void


                  {
                      // An XML list that contains both book nodes.               
                      a = myBooks.book[0].author; 
                  }
              ]]>
          </mx:Script>


      .....

      ....

                <mx:Button label="Defined" width="100%" height="34" textAlign="left" id="geospatial_defined_b"                     click="myViewStack.selectedChild=geospatial_defined_vs;"/>

      ....

      ...

                              <mx:ViewStack id="myViewStack" width="100%" height="358">
                                  <mx:Canvas  id="geospatial_defined_vs" label="geospatial_defined_vs" width="100%" height="100%">
                                      <mx:Text text="{'a: ' + a}" width="300"/>
                                  </mx:Canvas>

                              </mx:ViewStack>

       

      Can anyone hint me what should I do to read same data present in a xml file? Any link which answers the same query will be helpful too.

       

      Thank you

      -Bifz

        • 1. Re: Reading xml file
          babo_ya Level 3

          Don't quiet understand your question.. but, try this..

           

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

          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

                  layout="vertical"

                  verticalAlign="left"

                  backgroundColor="white"

                  creationComplete="init();"

          >

           

              <mx:Script>

              <![CDATA[

             

              private function init():void

              {

              txt.text = xml.book[0].title + " : " + xml.book[0].author;

              }

             

             

              ]]>

              </mx:Script>

           

              <mx:XML source="src/test.xml" id="xml" />

           

              <mx:Text id="txt" />

           

          </mx:Application>

           

           

          hope this helps,

           

          BaBo,

          • 2. Re: Reading xml file
            Bifz Level 1

            Thanks a lot. it worked. Even though you dint get the question, u gave the right solution. I am sorry for awfull descriptions of my problem.

            Now i can read data from a xml file and display it as text in my webapp.

             

            But, I have one more question. I uploaded the swf file and xml on the internet. Now, if i change the xml file, the changes do not appear in the text box in my webapp. It only works, if I "rebuild" the file and upload it. Is there anyway, that the changes appear on the webapp without having to rebuild it?

             

            Thanks. And I hope I explained the the question better this time.

             

            -Bifz

            • 3. Re: Reading xml file
              babo_ya Level 3

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

              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

                      layout="vertical"

                      verticalAlign="left"

                      backgroundColor="white"

                      creationComplete="init();"

              >

               

                  <mx:Script>

                  <![CDATA[

               

                 import flash.net.URLLoaderDataFormat;

               

                 private var urlRequest:URLRequest = new URLRequest("src/test.xml");

               

                  private function init():void

                  {

                 

               

                  var urlLoader:URLLoader = new URLLoader(urlRequest);

                  urlLoader.dataFormat = URLLoaderDataFormat.BINARY;

                  urlLoader.addEventListener(Event.COMPLETE, onLoadCompleted);

               

               

                  }

                  private function onLoadCompleted(event:Event):void

                  {

                  var loader:URLLoader = URLLoader(event.target);

                  var xml:XML = XML(loader.data);

                  txt.text = xml.book[0].title + " : " + xml.book[0].author;

                  }

               

               

                  ]]>

                  </mx:Script>

               

                  <mx:Text id="txt" />

               

              </mx:Application>

               

               

               

              Hope this helps,

               

              if this post answer your question please mark as "answer"

               

              BaBo,