10 Replies Latest reply on Jul 10, 2008 11:53 AM by cesaregb

    HTTPservice with XML

    cesaregb
      Hi all,

      I have a little question here, I have a <HTTPService ... tag wich brings a XML file, but Im adding this xml to a list by the sourse of it, but also I wanto to handle the information of it like a xml and when I change the resultFormat on the httpService to e4x to can handle it as xml the list doesn't take it, or doesn't show anything.

      and I want to know if someone can tell me how to solve my little problem ja, telling me how to make the list item take the data from the httpservice as e4x or how to conver the Object that it's returning this to a xml, I meen if there is any cast to xml

      any of this make sence ??

      well I hope any of u can help me on this

      Best Regards
      Cesar
        • 1. Re: HTTPservice with XML
          Gregory Lafrance Level 6
          This code might help. At least it shows how I load XML data and then convert it to an Object and then access the data.
          • 2. Re: HTTPservice with XML
            cesaregb Level 1
            It works but in this case i have this :


            <mx:HTTPService id="customersEmployees" method="GET" url=" http://localhost/vacations/customer_employees.php"/>
            .....
            public function setArrays(evt:ResultEvent):void
            {
            Alert.show(String(evt.result));
            xmlCustomers = evt.result as XML;
            //Alert.show(String(evt.result));
            Alert.show("1... "+String(xmlCustomers.elements().length()));
            }
            .....

            <mx:FormItem label="Clientes">
            <mx:List width="160" id="id_customers" height="78" dataProvider="{customersEmployees.lastResult.object.item}">
            <mx:itemRenderer>
            <mx:Component>
            <mx:VBox horizontalScrollPolicy="off" verticalScrollPolicy="off">
            <mx:Text text="{data.name}"/>
            </mx:VBox>
            </mx:Component>
            </mx:itemRenderer>
            </mx:List>
            </mx:FormItem>


            and in the actionscript function i want to handle the result as xml but if I change the resultFormat="e4x" the list for some reason aren't showing me the content ... that shows when i left it like that
            • 3. Re: HTTPservice with XML
              Gregory Lafrance Level 6
              What if you do this:

              [Bindable]
              private var resultObj:Object = new Object();

              private function resultHandler(event:ResultEvent):void{
              resultObj = Object(event.result);
              }

              and then this:

              dataProvider="{resultObj.object.item}">

              • 4. Re: HTTPservice with XML
                cesaregb Level 1
                I did it and still doesn't work =(

                i dont know why when I set the resulFormat="e4x" the list doesn't work and without it ... does work

                the list its like this

                <mx:List width="160" id="id_areas" height="78" dataProvider="{areasObj.object.item}">
                <mx:itemRenderer>
                <mx:Component>
                <mx:VBox horizontalScrollPolicy="off" verticalScrollPolicy="off">
                <mx:Text text="{data.name}"/>
                </mx:VBox>
                </mx:Component>
                </mx:itemRenderer>
                </mx:List>

                where the areasObj its the areasObj = Object(evt.result); of the eventHandler

                so i think its something like this but dont know what else

                i you could give me a little more of help at here .. i'll be glad ja =P


                best regards
                • 5. HTTPservice with XML
                  Gregory Lafrance Level 6
                  Please post sample data with about 4 full data elements. Please post in the same form the data appears in the actual data (including root element).
                  • 6. Re: HTTPservice with XML
                    cesaregb Level 1
                    this is te httpservice
                    <mx:HTTPService id="areasEmployees" method="GET" url=" http://localhost/vacations/areas_employees.php" resultFormat="e4x" result="setArrays(event);" fault="FaultHandler(event);"/>

                    the function
                    [Bindable]
                    private var areasObj:Object = new Object();

                    public function setArrays(evt:ResultEvent):void
                    {
                    Alert.show(String(evt.result));
                    areasObj = Object(evt.result);
                    }

                    the list
                    <mx:List width="160" id="id_areas" height="78" dataProvider="{areasObj.object.item}">
                    <mx:itemRenderer>
                    <mx:Component>
                    <mx:VBox horizontalScrollPolicy="off" verticalScrollPolicy="off">
                    <mx:Text text="{data.name}"/>
                    </mx:VBox>
                    </mx:Component>
                    </mx:itemRenderer>
                    </mx:List>

                    and the xml file its a php but this give you this:
                    <object>
                    <item>
                    <id>1</id>
                    <id_area>1</id_area>
                    <name>primero</name>
                    </item>
                    <item>
                    <id>2</id>
                    <id_area>1</id_area>
                    <name>primero</name>
                    </item>
                    </object>

                    I hope with this you can help me

                    it is this what u need to help me ??
                    • 7. Re: HTTPservice with XML
                      cesaregb Level 1
                      The list object can have like dataProvider an ArrayCollection isn't ?

                      then can I cast the e4x result from the httpservice into a ArrayCollection and with it use it as dataProvider for the list

                      and if I can, how can I do this ??

                      Also I've been looking for a way to load the XML page with ActionScript and I found that it should be something like this

                      var myXML:XML = new XML();
                      var XML_URL:String = "Sample.xml";
                      var myXMLURL:URLRequest = new URLRequest(XML_URL);
                      var myLoader:URLLoader = new URLLoader(myXMLURL);
                      myLoader.addEventListener("complete", xmlLoaded);

                      function xmlLoaded(event:Event):void
                      {
                      myXML = XML(myLoader.data);
                      Alert.show(myXML);
                      }

                      and the question here is, can I do this like a function that has a xml Return sending the path for the xml file and retriving the xml information, I mean like function xmlLoader(path):XML { ... } I tryed but dont know why it return me a null but in the alert in the xmlLoaded function does show me the content right.

                      And if I can do this, in which way it's better load the xml ?? by the <mx:HTTPService... or with ActionScript ?? which one you advice me ??

                      Regards
                      • 8. Re: HTTPservice with XML
                        EDendramis Level 1
                        When using e4x it SKIPS the root of the xml so you have to alter your parsing.

                        mx:HTTPService should work just fine. Set the resultFormat to "e4x". )This the the fastest and least memory intensive way to load xml without using AMF)

                        lets say you have this:


                        data:
                        ----------------------------------
                        <?xml version="1.0" encoding="utf-8"?>
                        <root>
                        <somenode>
                        <child id="fu" />
                        <child id="bar" />
                        </somenode>
                        </root>
                        ----------------------------------

                        <mx:HTTPService id="dataService"
                        resultFormat="e4x" url="data.xml" result="gotdata(event)" />

                        private function gotdata(e:ResultEvent):void
                        {
                        var tempXML:XML = new XML(e.result);
                        trace(tempXML.somenode); //THIS WORKS
                        trace(tempXML.root.somenode); //THIS DOES NOT WORK WITH e4x

                        }
                        • 9. Re: HTTPservice with XML
                          Gregory Lafrance Level 6
                          That's right. This works:
                          • 10. Re: HTTPservice with XML
                            cesaregb Level 1
                            It works


                            thanks a lot


                            Best Regars