7 Replies Latest reply on Apr 1, 2010 6:08 PM by avdave

    Retrieve data from XML

    avdave

      Hello,

         I am new to this forum.  I have an XMl document as below:

       

      <gallery>

                  <image>

                  <title>pic1</title>

                  <text> txt1 <text>

                 </image>

                <image>

                  <title>pic2</title>

                   <text> txt2 <text>

                 </image>

      </gallery>

       

       

       

      I want to fetch the value of "text" node and print it.Can anyone help?

       

      -Akshay

        • 1. Re: Retrieve data from XML
          Vikash.kumar29 Level 3

          Hello Akshay you can use following code to access each text node:

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" horizontalAlign="center">
              <mx:Script>
                  <![CDATA[
                  private var gallaryXML:XML=<gallery>
                                              <image>
                                              <title>pic1</title>
                                                   <text> txt1 </text>
                                             </image>
                                            <image>
                                                  <title>pic2</title>
                                                   <text> txt2 </text>
                                             </image>
                                          </gallery>;
                 
                  private function printText():void
                  {
                      var length:int = (gallaryXML.image as XMLList).length();
                      textTA.text = "";
                      for(var index:int = 0;index<length;index++)
                      {
                          textTA.text = textTA.text + "\n" +     gallaryXML.image[index].text;
                      }
                  }
                     
                  ]]>
              </mx:Script>
              <mx:Button click="printText()" label="Print Text Value">
                 
              </mx:Button>
              <mx:TextArea id="textTA"  height="87" width="226"/>
          </mx:Application>

           

          Hope this helps you

          Cheers

          -Vikash

          1 person found this helpful
          • 2. Re: Retrieve data from XML
            Matt Le Fevre Level 4

            avdave wrote:

             

            Hello,

               I am new to this forum.  I have an XMl document as below:

             

            <gallery>

                        <image>

                        <title>pic1</title>

                        <text> txt1 <text>

                       </image>

                      <image>

                        <title>pic2</title>

                         <text> txt2 <text>

                       </image>

            </gallery>

             

             

             

            I want to fetch the value of "text" node and print it.Can anyone help?

             

            -Akshay

             

            how are you using the data?

             

            is it being used in a standalone component? or in an item renderer?

             

            how are you accessing the data?

             

            is it build into the application in an XML list? or are you retrieving it via PHP? or an external XML file?

            • 3. Re: Retrieve data from XML
              avdave Level 1

              Hello Vikash,

                 Thanks for your answer. I found it useful. In the mean time I tried to access XML using HTTP Service and the bounded it in Array collection as below:

               

              <![CDATA[
                          import mx.collections.ArrayCollection;
                          import mx.rpc.events.FaultEvent;
                          import mx.rpc.events.ResultEvent;
                         
                          
                          [Bindable]
                          public var galleryData:ArrayCollection;
                          
                         
                          
                          protected function  resultHandler(event:ResultEvent):void
                          {
                              //getting XML in array collection

                                 galleryData = event.result.gallery.images;
                             
                             
                             
                          }
                         
                          // fault event handler for employeeService HTTPService call
                          private function faultHandler(event:FaultEvent):void {
                              // Add code here for what to do when there is an error retrieving data
                          }
                      ]]>

               

                  </fx:Script>
              <fx:Declarations>   
                  <mx:HTTPService id="galleryService"
                                  url="data/gallery.xml"
                                  result="resultHandler(event)"
                                  fault="faultHandler(event)"/>
              </fx:Declarations>  

               

               

              and I am struggeling to get individual values from that array collection. But your response helps. Thanks!

              -Akshay

              • 4. Re: Retrieve data from XML
                avdave Level 1

                Hello,

                    Thanks for your reply.

                 

                     I need to retireve the image soruce and image text dynamically from xml. For that I am trying to read the XML using Http Request and then in result handler I am getting it in Array Collection: but afterwards I am strugelling to get individual element from array collection: here's my code:

                 

                <![CDATA[
                             import mx.collections.ArrayCollection;
                             import mx.rpc.events.FaultEvent;
                             import mx.rpc.events.ResultEvent;
                            
                             
                             [Bindable]
                             public var galleryData:ArrayCollection;
                             
                            
                             
                             protected function  resultHandler(event:ResultEvent):void
                             {
                                 //getting XML in array collection

                                   galleryData = event.result.gallery.images;

                 

                               //tried getItemAt (index) but it's not working not sure how to progress further
                                
                                
                                
                             }
                            
                             // fault event handler for employeeService HTTPService call
                             private function faultHandler(event:FaultEvent):void {
                                 // Add code here for what to do when there is an error  retrieving data
                             }
                         ]]>

                 

                    </fx:Script>
                <fx:Declarations>   
                     <mx:HTTPService id="galleryService"
                                     url="data/gallery.xml"
                                     result="resultHandler(event)"
                                     fault="faultHandler(event)"/>
                </fx:Declarations>  

                 

                 

                and I am  struggeling to get individual values from that array collection. tried getItemAt (index) but it's not working not sure how to progress  further

                -Akshay

                • 5. Re: Retrieve data from XML
                  avdave Level 1

                  Also,

                    Can you please tell that how would you load XML through HttpReuqest in gallaryXMl ariable instead of hard coding.

                   

                     Sorry if my question sounds naive but I am really new to flex builder and struggeling for my app.

                   

                   

                  Thanks

                  -Akshay

                  • 6. Re: Retrieve data from XML
                    Matt Le Fevre Level 4

                    i have a very basic example of loading XML information into an application here:

                     

                    http://www.mattlefevre.com/viewExample.php?tut=flex&proj=Basic%20Gallery

                     

                    with a little bit of tweaking and modification it could be altered to also store text information as well , it should get you started at least

                    • 7. Re: Retrieve data from XML
                      avdave Level 1

                      Thanks very much, this is what I was looking for !