4 Replies Latest reply on May 28, 2009 12:59 AM by mangemuck

    Single XML result

    mangemuck

      Hey

       

      I started out with Flex two weeks ago, and I'm trying to build a small ERP. I've managed to print out lists with data (in datagrid), but how do I convert an XML page with only one result into the textinputs I have in my editing-section?

       

      This is what I have.

       

      Backend - getCst.php:

      <?php
      include("../../../PHP/connectToDb.php");
      
      $query = "SELECT * FROM `".$dbname."`.`cstlst` WHERE `ID`=".$_REQUEST['id'];
      $result=mysql_query($query);
      
      echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
      echo "<Cst>\n";
      while($r = mysql_fetch_array($result, MYSQL_ASSOC))
      {
           echo " <CstInfo ID=\"".$r['ID']."\">\n";
           foreach ($r as $k => $v) {
                    echo "  <".$k.">".$v."</".$k.">\n";
           }
           echo " </CstInfo>\n";
      }
      echo "</Cst>\n";
      ?>
      

      Result of backend:

      <?xml version="1.0" encoding="UTF-8"?>
      <Cst>
       <CstInfo ID="5">
        <ID>5</ID>
        <ordFirst>Magnus</ordFirst>
        <ordLast>Johansson</ordLast>
        <ordCompany></ordCompany>
        <ordStreet>Example street 12</ordStreet>
        ...
       </CstInfo>
      </Cst>
      

       

      So far everything seems correct, because I can view the correct result in a browser. However, when I'm trying to access the results from Flex it doesn't work.

       

      Frontend - editCst.mxml:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="100%" height="100%" initialize="getCst.send();">
      
           <mx:Script>
                <![CDATA[
                import mx.collections.ArrayCollection;
                import mx.utils.ObjectProxy;
                import mx.utils.ArrayUtil;
                import mx.rpc.events.ResultEvent;
                import mx.rpc.events.FaultEvent;
                import mx.controls.Alert;
      
                [Bindable]
                private var CstList:ArrayCollection;
                public function handleXML(event:ResultEvent):void {
                     if (event.result.Cst.CstInfo is ArrayCollection) {
                          CstList = event.result.Cst.CstInfo;
                     } else if (event.result.Cst.CstInfo is ObjectProxy) {
                          CstList = new ArrayCollection(ArrayUtil.toArray(event.result.Cst.CstInfo)); 
                     }
                     Alert.show(CstList.getItemAt(0).getItemAt(0).toString(),"Success");
                }
      
                public function handleFault(event:FaultEvent):void {
                   Alert.show(event.fault.faultString, "Error");
                }
      
                ]]>
           </mx:Script>
      
           <mx:HTTPService result="handleXML(event);" fault="handleFault(event);" id="getCst" url="Modules/CstMgmnt/editCst/getCst.php">
                <mx:request xmlns="">
                   <id>{idL.text}</id>
               </mx:request>
           </mx:HTTPService>
      
           <mx:Panel x="0" y="0" width="309" height="130" layout="absolute" title="Orderer" id="orderer">
                <mx:Label x="10" y="10" text="Name:" width="70" height="22" textAlign="right" id="ordNameL"/>
                <mx:TextInput x="80" y="10" id="ordFirst" width="100" height="22"/>
                <mx:TextInput x="182" y="10" id="ordLast" width="100" height="22"/>
                <mx:Label x="10" y="35" text="Company:" width="70" height="22" textAlign="right" id="ordCompanyL"/>
                <mx:TextInput x="80" y="35" id="ordCompany" width="202" height="22"/>
                <mx:Label x="10" y="60" text="Address:" width="70" height="22" textAlign="right" id="ordAddressL"/>
                <mx:TextInput x="80" y="60" id="ordStreet" width="202" height="22"/>
           </mx:Panel>
      
           <mx:Label x="10" y="140" text="ID:" id="idL"/>
           <mx:TextInput x="41" y="138" width="76" id="idText" change="getCst.send();" text="5"/>
      
      </mx:Module>
      

       

      Error - The above code gives me the error:

      TypeError: Error #1009: Cannot access a property or method of a null object reference.
           at Modules.CstMgmnt.editCst::test/handleXML()[C:\Users\Magnus\Documents\Flex Builder 3\Uflex ERP\src\Modules\CstMgmnt\editCst\test.mxml:21]
           at Modules.CstMgmnt.editCst::test/__getCst_result()[C:\Users\Magnus\Documents\Flex Builder 3\Uflex ERP\src\Modules\CstMgmnt\editCst\test.mxml:31]
           at flash.events::EventDispatcher/dispatchEventFunction()
           at flash.events::EventDispatcher/dispatchEvent()
           at mx.rpc.http.mxml::HTTPService/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\http\mxml\HTTPService.as:290]
           at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:193]
           at mx.rpc::Responder/result()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\Responder.as:43]
           at mx.rpc::AsyncRequest/acknowledge()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\AsyncRequest.as:74]
           at DirectHTTPMessageResponder/completeHandler()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\messaging\channels\DirectHTTPChannel.as:403]
           at flash.events::EventDispatcher/dispatchEventFunction()
      

       

       

      I appreciate any help on this problem.

      /Magnus

        • 1. Re: Single XML result
          _Natasha_ Level 4

          Hi,

          try to set resultFormat for HTTPService to e4x or xml.

          Check the result dataType in debug mode

           

          public function handleXML(event:ResultEvent):void {
                       & nbsp; if (event.result.Cst.CstInfo is ArrayCollection) {
                       & nbsp;      CstList = event.result.Cst.CstInfo;
                       & nbsp; } else if (event.result.Cst.CstInfo is ObjectProxy) {
                       & nbsp;      CstList = new ArrayCollection(ArrayUtil.toArray(event.result.Cst.CstInfo)); 
                       & nbsp; }
                       & nbsp; Alert.show(CstList.getItemAt(0).getItemAt(0).toString(),Success);
                    }
          
          This way CstList may be a null.

          • 2. Re: Single XML result
            oldMster Level 3

            Also, the XML start/stop tags (<Cst> in this case), aren't included in the address.

            event.result.CstInfo is the correct address, not event.result.Cst.CstInfo

            1 person found this helpful
            • 3. Re: Single XML result
              mangemuck Level 1

              Thanks for your anwsers.

               

              I just noticed one big error in my code. The request was sending the value of the LABEL text of ID ("ID:") and not the textinput's value! That's why I always got a null-value returned.

               

              I have changed the resultformat to e4x. But how do I get the data extracted from the document? I realized that I may not need the if-part below, just get the data from getCst.lastResult instead. But this gives me nothing but the title "Success".

                        public function handleXML(event:ResultEvent):void {
                             if (event.result.Cst.CstInfo is ArrayCollection) {
                                  CstList = event.result.Cst.CstInfo;
                             } else if (event.result.Cst.CstInfo is ObjectProxy) {
                                  CstList = new ArrayCollection(ArrayUtil.toArray(event.result.Cst.CstInfo)); 
                             }
              
                             Alert.show(getCst.lastResult.Cst.CstInfo.ordFirst.toString(),"Success");
                        }
              

               

              Thanks for your help so far.

              /Magnus

              • 4. Re: Single XML result
                mangemuck Level 1

                I googled around more yesterday, and found this link:

                 

                http://blog.flexexamples.com/2007/09/19/converting-xml-to-objects-using-the-flex-httpservi ce-mxml-tag/

                 

                Thanks for your help!

                /Magnus