17 Replies Latest reply on Oct 24, 2009 3:50 AM by Michael Orzechowski

    Conversion of string into XML object

    Michael Orzechowski Level 1

      Hi

       

      I am having some problems with conversion of string (containing XML data) into Flex XML object and binding it later to UI elements to output/maintain this data.

      Binding of XML structure to UI elements works perfectly fine if I will do following:

      1)      Hardcode XML object within Flex file

      2)      Read xml file from repository (xml file inside the Flex project)

      3)      Use HTTP request to retrieve XML data

      Unfortunately none of the above scenarios suits my solution.

      I am developing a prototype application for processing Flex forms inside SAP system. I have decided to make data bindings using XML structure stored in Data Base. When rendering form inside web browser based application I am retrieving corresponding XML schema (empty for new forms and populated for saved forms) and pass it to Flex form as a string type import parameter. Data is being passed correctly (I can display it on TextArea control for instance) but after conversion to XML and binding to DataGrid I am not getting any results.

      I am converting string (containing XML) to XML object in following way:

      Private var xml_obj:XML = new XML(string_xml );

      I am catching any potential errors but conversion is going well. After conversion I am not getting any results after binding it to DataGrid control and I am not able to access any of the nodes using AS code either. At the same time variable xml_obj is not empty (not null).

       

      Any help would be much appreciated.

       

      Regards

      Michael

        • 1. Re: Conversion of string into XML object
          Flex harUI Adobe Employee

          Use toXMLString() to see what the results of the conversion are.  Post the results if you still have questions.  There might be an issue with namespaces.

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: Conversion of string into XML object
            Michael Orzechowski Level 1

            Hi Alex

             

            I did as you suggested. After conversion of XML object to string using toXMLString function I am getting empty string. Take a look at the code below:

            private function setParameters():void

            {

                                                           

                            try

                            {

                                            xml_obj=new XML(xml_data_string);

                            }

                            catch (error:Error)

                            {

                                            Alert.show(error.toString());

                            }

                                                                                           

                            xml_data_string_2 = xml_obj.toXMLString();                     

                                                                                                                            

            }

             

            I tried to bind variables xml_data_string and xml_data_string_2 to TextAreas but only first one (bound to xml_data_string) is being populated with XML.

            Obviously binding of xml_obj to UI element like DataGrid does not work either.

            Please find example (copied from some example) XML structure I am importing to Flex:

             

            <CATALOG>

                            <PLANT>

                                            <COMMON>Bloodroot</COMMON>

                                            <BOTANICAL>Sanguinaria canadensis</BOTANICAL>

                                            <ZONE>4</ZONE>

                                            <LIGHT>Mostly Shady</LIGHT>

             

                                            <PRICE>$2.44</PRICE>

                                            <AVAILABILITY>031599</AVAILABILITY>

                            </PLANT>

                            <PLANT>

                                            <COMMON>Columbine</COMMON>

                                            <BOTANICAL>Aquilegia canadensis</BOTANICAL>

                                            <ZONE>3</ZONE>

             

                                            <LIGHT>Mostly Shady</LIGHT>

                                            <PRICE>$9.37</PRICE>

                                            <AVAILABILITY>030699</AVAILABILITY>

                            </PLANT>

            </CATALOG>

             

            I also tried to remove all spaces/tabs from XML file but it did work either.

            Please helpJ

             

            Regards

            Michael

            • 3. Re: Conversion of string into XML object
              David_F57 Level 5

              I'm not sure how you are passing your data into flex but here is one way to get a string into a grid. Basically its one step more than what you have been doing, I convert the string to xml then the xml to a xmllistcollection which binds cleanly to the grid.

               

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

              <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                 xmlns:s="library://ns.adobe.com/flex/spark"

                 xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768">

              <fx:Declarations>

              <s:XMLListCollection id="myXML"/>

              </fx:Declarations>

               

              <fx:Script>

              <![CDATA[

               

              private var xml:XML;

               

              private function getXML(): void

              {

              xml= new XML('<CATALOG>'+

              '<PLANT>'+

              '<COMMON>Bloodroot</COMMON>'+

              '<BOTANICAL>Sanguinaria canadensis</BOTANICAL>'+

              '<ZONE>4</ZONE>'+

              '<LIGHT>Mostly Shady</LIGHT>'+

              '<PRICE>$2.44</PRICE>'+

              '<AVAILABILITY>031599</AVAILABILITY>'+

              '</PLANT>'+

              '<PLANT>'+

              '<COMMON>Columbine</COMMON>'+

              '<BOTANICAL>Aquilegia canadensis</BOTANICAL>'+

              '<ZONE>3</ZONE>'+

              '<LIGHT>Mostly Shady</LIGHT>'+

              '<PRICE>$9.37</PRICE>'+

              '<AVAILABILITY>030699</AVAILABILITY>'+

              '</PLANT>'+

              '</CATALOG>');

               

              myXML = new XMLListCollection(xml.*);

              }

               

              ]]>

              </fx:Script>

              <s:Button x="62" y="26" label="Button" click="getXML()"/>

              <mx:DataGrid id="dg" x="62" y="90" dataProvider="{myXML}">

              <mx:columns>

              <mx:DataGridColumn headerText="Column 1" dataField="COMMON"/>

              <mx:DataGridColumn headerText="Column 2" dataField="BOTANICAL"/>

              <mx:DataGridColumn headerText="Column 3" dataField="ZONE"/>

              </mx:columns>

              </mx:DataGrid>

              </s:Application>

              1 person found this helpful
              • 4. Re: Conversion of string into XML object
                Michael Orzechowski Level 1

                David

                 

                First of all sorry for not stating it clearly but I am using Flex 3 for this development (at the moment it is the only choice when embedding Flex objects inside SAP applications).

                You must have missed the bit where I am describing how this XML data finds its way inside Flex. I am passing it to Flex as String type parameter during rendering (directly from DB where it is stored).

                Now, following code works perfect (XML is embedded inside Flex project):

                                <mx:XML id="form_data" source="../assets/example_xml_data.xml"/>

                                                                               

                                <mx:Script>

                                                <![CDATA[

                 

                                                                import mx.collections.XMLListCollection;

                                                                import mx.controls.Alert;

                 

                                                                [Bindable]

                                                                public var XML_list:XMLListCollection;

                                                               

                                                                private function setParameters():void

                                                                {

                                                                               XML_list = new XMLListCollection(form_data.*);             

                                                                }                                             

                                                               

                                                ]]>

                                </mx:Script>

                               

                                <mx:DataGrid id="myDataGrid" dataProvider="{XML_list}">

                                               

                                                <mx:columns>

                                                                <mx:DataGridColumn dataField="COMMON" headerText="Popular name"/>

                                                                <mx:DataGridColumn dataField="BOTANICAL" headerText="Botanical name"/>

                                                                <mx:DataGridColumn dataField="ZONE" headerText="Zone"/>

                                                                <mx:DataGridColumn dataField="LIGHT" headerText="Light"/>                                                                                                                                               

                                                                <mx:DataGridColumn dataField="PRICE" headerText="Price"/>                                               

                                                                <mx:DataGridColumn dataField="AVAILABILITY" headerText="Availability"/>                                    

                                                </mx:columns>               

                                               

                                </mx:DataGrid>

                 

                 

                But following code does not work (XML passed to Flex form as String input parameter):

                 

                import sap.FlashIsland;

                import mx.controls.Alert;

                import mx.collections.XMLListCollection;

                                                                               

                [Bindable]

                public var xml_data:String;

                 

                private var form_data:XML;

                 

                [Bindable]

                private var XML_list:XMLListCollection;

                                                               

                private function initApp():void

                {

                                FlashIsland.register(this);

                }

                                                               

                private function setParameters():void

                {

                                                               

                                try

                                {

                                                form_data=new XML(xml_data);

                                }

                                catch (error:Error)

                                {

                                                Alert.show(error.toString());

                                }

                                                                               

                                  XML_list = new XMLListCollection(form_data.*);           

                                                                                                                                

                }

                 

                 

                XML string does find its way inside Flex form. I can display content of variable xml_data in TextArea and all looks fine. Conversion to XML (variable form_data) goes well (no error)

                 

                 

                Please helpJ

                 

                Regards

                Michael

                • 5. Re: Conversion of string into XML object
                  mewk Level 3

                  dunno, is working for me. make sure the data provider for your datagrid is updated on changes to your string data.

                   

                  - e

                  • 6. Re: Conversion of string into XML object
                    David_F57 Level 5

                    Ok,

                     

                    in your string load routine dettach the provider and reattach that helps sometimes with Flex3.

                     

                    i.e. mylist = new xmlListCollection(stuff);

                    mygrid.dataprovider=null;

                    mygrid.dataprovider=mylist.

                     

                    David

                    1 person found this helpful
                    • 7. Re: Conversion of string into XML object
                      David_F57 Level 5

                      Ok,

                       

                      in your string load routine dettach the provider and reattach that helps sometimes with Flex3. I realize you are retrieving a string but it was just how it gets there that I was wondering about. If I have a request to sap for some data then there is a period of time before that data gets back, this can basically see the display controls miss a data update(the reason why loadcomplete is very important for displaying retrieved data).

                       

                      i.e. mylist = new xmlListCollection(stuff);

                      mygrid.dataprovider=null;

                      mygrid.dataprovider=mylist.

                       

                      David

                      • 8. Re: Conversion of string into XML object
                        Michael Orzechowski Level 1

                        Guys

                         

                         

                        I think I am going crazy here...

                         

                        I did as you suggested David, I have added following function triggered by the button on the form:

                         

                          private function reload_XML():void
                          {
                            Alert.show("Reloading XML data...");
                            myDataGrid.dataProvider=null;
                            myDataGrid.dataProvider=XML_list;
                            Alert.show("...done");   
                          }

                         

                        Unfortunately it still does not work.

                        I have added messages to make sure it is exacuted and imagine my surprise after getting message "...done" first and next message "Reloading XML data..." when pressing the button. What the heck is going on with this Flex:)

                         

                        Mewk, I wanted to look at your example but I am getting message that file is corrupted when I am trying to extract it.

                         

                         

                        Regards

                        Michael

                        • 9. Re: Conversion of string into XML object
                          David_F57 Level 5

                          how are you actually retrieving the string ? is it a call to a sap API. and if so does it have some sort of complete listener available ?

                           

                          David

                          • 10. Re: Conversion of string into XML object
                            mewk Level 3

                            Michael,

                             

                            File shouldn't be corrupted (just checked it!). But here it is again:

                             

                             

                            <?xml version="1.0" encoding="utf-8"?>
                            <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                              xmlns:s="library://ns.adobe.com/flex/spark" 
                              xmlns:mx="library://ns.adobe.com/flex/halo" >
                            
                              <fx:Declarations>
                                <fx:XML id="formData" source="data/xml_data.xml" />
                              </fx:Declarations>
                            
                              <fx:Script>
                                <![CDATA[
                                  import mx.collections.XMLListCollection;
                                  import mx.controls.Alert;
                                  import mx.events.FlexEvent;
                            
                                  [Bindable] public var xmlList:XMLListCollection;
                            
                                  protected function myDataGrid_creationCompleteHandler(event:FlexEvent):void {
                                    xmlList = new XMLListCollection(formData.*);
                                  }
                            
                            
                                  //import sap.FlashIsland;
                                           
                                  [Bindable] public var xmlString1:String = "<catalog>" +
                                                         "<plant>" +
                                                            "<common>tree of life</common>" +
                                                            "<botanical>Arbor vitae</botanical>" +
                                                            "<zone>777</zone>" +
                                                            "<light>mostly shady</light>" +
                                                            "<price>$0.00</price>" +
                                                            "<availability>1</availability>" +
                                                         "</plant>" +
                                                      "</catalog>";
                                  [Bindable] public var xmlString2:String = 
                                                      "<catalog>" +
                                                         "<plant>" +
                                                            "<common>tree of knowledge</common>" +
                                                            "<botanical>Etz haDaat tov V'ra</botanical>" +
                                                            "<zone>eden</zone>" +
                                                            "<light>mostly shady</light>" +
                                                            "<price>$0.00</price>" +
                                                            "<availability>1</availability>" +
                                                         "</plant>" +
                                                         "<plant>" +
                                                            "<common>Arbre du Ténéré</common>" +
                                                            "<botanical>Acacia tortilis</botanical>" +
                                                            "<zone>Sahara</zone>" +
                                                            "<light>stark</light>" +
                                                            "<price>$0.00</price>" +
                                                            "<availability>0</availability>" +
                                                         "</plant>" +
                                                       "</catalog>";
                                  [Bindable] private var xmlList2:XMLListCollection;
                                  private var formData2:XML;
                            
                            
                                  /* private function initApp():void {
                                        FlashIsland.register(this);
                                     } */
                            
                                  private function xmlHandler(xmlDataString:String):void {
                                    try {
                                      formData2 = new XML(xmlDataString);
                                    } catch (error:Error) {
                                      Alert.show(error.toString());
                                      return;
                                    }          
                                      xmlList2 = new XMLListCollection(formData2.*);           
                                    }
                            
                                  ]]>
                                </fx:Script>
                            
                                <s:VGroup verticalCenter="0" horizontalCenter="0">
                            
                                <mx:DataGrid id="myDataGrid" dataProvider="{xmlList}" 
                                             creationComplete="myDataGrid_creationCompleteHandler(event)">
                                  <mx:columns>
                                    <mx:DataGridColumn dataField="common" headerText="Popular name"/>
                                    <mx:DataGridColumn dataField="botanical" headerText="Botanical name"/>
                                    <mx:DataGridColumn dataField="zone" headerText="Zone"/>
                                    <mx:DataGridColumn dataField="light" headerText="Light"/>                                                                                                                                               
                                    <mx:DataGridColumn dataField="price" headerText="Price"/>                                               
                                    <mx:DataGridColumn dataField="availability" headerText="Availability"/>                                    
                                  </mx:columns>               
                                </mx:DataGrid>
                            
                            
                                <mx:DataGrid id="myDataGrid2" dataProvider="{xmlList2}" 
                                             creationComplete="xmlHandler(xmlString1)">
                                  <mx:columns>
                                    <mx:DataGridColumn dataField="common" headerText="Popular name"/>
                                    <mx:DataGridColumn dataField="botanical" headerText="Botanical name"/>
                                    <mx:DataGridColumn dataField="zone" headerText="Zone"/>
                                    <mx:DataGridColumn dataField="light" headerText="Light"/>                                                                                                                                               
                                    <mx:DataGridColumn dataField="price" headerText="Price"/>                                               
                                    <mx:DataGridColumn dataField="availability" headerText="Availability"/>                                    
                                  </mx:columns>               
                                </mx:DataGrid>
                            
                                <s:Button label="change xml string data"
                                          click="xmlHandler(xmlString2)" />
                            
                              </s:VGroup>
                            
                            </s:Application>
                            

                             

                             

                            data/xml_data.xml

                             

                            <catalog>
                              <plant>
                                <common>bloodroot</common>
                                <botanical>sanguinaria canadensis</botanical>
                                <zone>4</zone>
                                <light>mostly shady</light>
                                <price>$2.44</price>
                                <availability>031599</availability>
                              </plant>
                              <plant>
                                <common>columbine</common>
                                <botanical>aquilegia canadensis</botanical>
                                <zone>3</zone>
                                <light>mostly shady</light>
                                <price>$9.37</price>
                                <availability>030699</availability>
                              </plant>
                            </catalog>
                            

                             

                             

                            I pretty much just took your code and ran with it. As has been mentioned previously, check that the datagrid is updated with changes to the data string.

                             

                            It would be helpful if we could see a bit more of your code. When the SAP updates the string, what listeners and event handlers do you have built to handle the new data results?? For example, in the code above, when you click the button, the xmlString is changed and there is a call to xmlHandler, which changes xmlList2 (which is bindable) and so the changes are reflected in the datagrid. Hope this helps,

                             

                            - e

                            • 11. Re: Conversion of string into XML object
                              David_F57 Level 5

                              hi Mewk,

                               

                              This is turning into one of those epic problems, I chatted with Michael earlier and it appears that the string is passed as a parameter from a call to the swf from another program, the string is actually there, it loads happily into a textarea field. It was tried to convert the textarea text to xml and that failed as well, Michael was going to install  FB beta 2 and see if the problem still occurs, in the meanwhile it is a weird mystery, I suggested looping through the string to strip out any possible non alpha numeric characters that sap may have put in it cuases the xml pareser to fail silently. Anyway time to just wait and see what happens under FB beta 2.

                               

                              David

                              • 12. Re: Conversion of string into XML object
                                mewk Level 3

                                Strange that the data is viewable in the TA box, but can't be manipulated successfully after that. Sounds like it's time to use the debugger and pin down the exact moment of fail.

                                 

                                Oh, and, this could be one of those epic problems or it could be one of those I-can't-believe-I-spent-ten-hours-on-this-frigg'n-bug-when-the-solution-was-staring-me-in -the-face-the-whole-time kind of thing.

                                 

                                - e

                                • 13. Re: Conversion of string into XML object
                                  David_F57 Level 5

                                  hi Mewk,

                                   

                                  debugging is the way to go, I have never really had much luck with remote debugging and i'm not the best source for advice on it, hows your skillset in this area..

                                   

                                  David.

                                  • 14. Re: Conversion of string into XML object
                                    Peter deHaan Level 4

                                    Re:

                                    I have added messages to make sure it is exacuted and imagine my surprise after getting message "...done" first and next message "Reloading XML data..." when pressing the button. What the heck is going on with this Flex:)

                                     

                                    Correct. "done" is displayed first and then "Reloading XML data" second. I believe that is the fun of using Alert's to debug. See, actually the "Reloading XML data" is displayed first, followed closely by "done", but since they are alerts, the last one is displayed on top of the previous alert. Debugging with Alerts can be very confusing since it appears that code is running in reverse order. You should be able to use trace() statements instead and everything should appear in the normal, expected order.

                                     

                                    Peter

                                    • 15. Re: Conversion of string into XML object
                                      Michael Orzechowski Level 1

                                      Hi guys

                                       

                                      Quick update (have loads of other work at the moment, can’t play with Flex as much as I would like to).

                                      I tried to recreate my example in new Flash builder (beta 2). Unfortunately I got some strange error when running it inside SAP. After further investigations it turned out that 3rd party libraries have to be one a same SDK level as Flex file (figures) so I am stuck with Flex 3 builder.

                                      It seems to me that there is something odd about XML string passed by SAP (David suggested that before). The reason why I am saying it is because I can copy paste content of XML directly into string variable and all works fine (which means that code it perfectly fine).

                                      Unfortunately I can’t use debugger so I have to result to primitive ways of finding errors.

                                       

                                      Peter, thanks for explaining how displaying messages with "show" function works. I will try to set a proper trace.

                                       

                                      Mewk, I will also try your example.

                                       

                                      Thanks you all for your contributions. Will keep you posted.

                                       

                                      Regards

                                      Michael

                                      • 16. Re: Conversion of string into XML object
                                        David_F57 Level 5

                                        try the string clean thing

                                         

                                        private function cleanString(value:String): String

                                        {

                                        var newString:String="";

                                        for (var i:int=0;i < value.length;i++)

                                        {

                                        if (myString.charCodeAt(i) > 31 && myString.charCodeAt(i) < 128) newString +=myString.charAt(i);

                                        }

                                        return myString;

                                        }

                                        David

                                        • 17. Re: Conversion of string into XML object
                                          Michael Orzechowski Level 1

                                          Guys

                                           

                                          Finally, weekend arrived so I had a chance to fiddle around with my test Flex file.

                                          First of all I have set up a trace and looked into a log file. Apparently XML string passed from SAP is empty when event “applicationComplete” occurs.

                                          Next I have decided to go back to David's idea with detaching and reattaching data provider. The only addition I’ve done was to generate XML list collection again (knowing that initially it has been created using empty string variable) before reattaching it. All works perfect (string cleanup was not necessary).

                                          The only thing is that it does not solve all my issues. I can’t expect user to click on a button to load data. It has to be done automatically. I will look into FlashIsland (SAP interface) component and its events (I already did but with no luck), perhaps it is possible to trigger attaching data provider to UI element on external variable load event.

                                           

                                          Nevertheless I would like to thank you all for all your suggestions. I am going to grant each one of you points for all the ideas you shared with me.

                                           

                                          Regards

                                          Michael