2 Replies Latest reply on Jun 7, 2009 7:33 AM by hvcardoso

    Reading from external XML source

    hvcardoso

      Hi all,

       

      I'm a noob at Flex but trying to learn.

       

      I encountered a problem that i can't seem to fix.

       

      I'm invoking an HTTPService like this:

       

       

       

       

      Header 1

      <mx:HTTPService

       

      id="searchName"

      url="

      {compexUrl}"

      result="searchHandler(event)"

      resultFormat = "

      e4x">

       

      <mx:request xmlns="">

       

      <api_key>xxxxxxxx</api_key>

       

      </mx:request>

      </mx:HTTPService>

       

      Where

      compexUrl =

      http://test/ + platform + "/" + searchTerms;

       

      My searchHandler is like this:

       

      [

      Bindable]

       

      public var item:XMLList = null;

       

       

      private function searchHandler(event:ResultEvent):void{

      item=

      new XMLList(event.result);

      }

       

      And the xml returned has the following aspect:

      <?xml version="1.0" encoding="UTF-8" ?>
      - <game>
      <id>20060418113640115000</id>
      - <name>
      <![CDATA[
      God Hand
      ]]>
      </name>
      </game>
      - <game>
      <id>2005120717012468975830</id>
      - <name>
      <![CDATA[
      God of War
      ]]>
      </name>
      </game>
      - <game>
      <id>20070510133243896018</id>
      - <name>
      <![CDATA[
      God of War Betrayal [mobile]
      ]]>
      </name>
      </game>
      - <game>
      <id>20060313151238825080</id>
      - <name>
      <![CDATA[
      God of War II
      ]]>
      </name>
      </game>
      - <game>
      <id>2005120717012341705322</id>
      - <name>
      <![CDATA[
      GoDai Elemental Force
      ]]>
      </name>
      </game>
      - <game>
      <id>2005120717012453155517</id>
      - <name>
      <![CDATA[
      Godzilla Save The Earth
      ]]>
      </name>
      </game>
      - <game>
      <id>20070918144248206042</id>
      - <name>
      <![CDATA[
      Godzilla: Unleashed
      ]]>
      </name>
      </game>
      </games>

       

      I want to populate my var item with all the values of the tag name but i can´t seem to figure out how?

       

      Can you help me please?

        • 1. Re: Reading from external XML source
          BennyBeta Level 4

          In your handler, try:


          item = event.result.game.name;

           

          Let me know if that helps...

           

          Ben Edwards

          • 2. Re: Reading from external XML source
            hvcardoso Level 1

            Hi thanks for your reply but thats not the answer, i've tried that already....

             

            My problem was that i had a namespace in my tags, aparently it changes the way we can access the attribute value

             

            <games xmlns="http://api.gamesradar.com">

             

            One way to deal with this namespace is to acces the variable like this:

            var ns:Namespace = new Namespace("http://api.gamesradar.com");

            item = XML(event.result).ns::game.*::name;

             

            After i solved this issue i stumble upon another one, i wanted to put those results in a dataGrid like this:

             

             

             

             

            <mx:DataGrid x="10" y="101" width="786" id="searchInfo" dataProvider="{item}">

            <mx:columns>

            <mx:DataGridColumn headerText="Título" dataField="name"/>

            <mx:DataGridColumn headerText="Identificador" dataField="id"/>

            </mx:columns>

            </mx:DataGrid>

             

             

             

            Well Flex doesn't let me do that so i followed another route.....i simply removed the namespace from my xml with this function:

             

             

            public function result(response:Object):XML{

            var xmlString:String;

            var xmlnsPattern:RegExp;

            var namespaceRemovedXML:String;

            var responseXML:XML;

            // get the XML payload

            if(ResultEvent(response).result is XMLList){

            response = ResultEvent(response).result

            as XMLList;

            }

            else{

            response = ResultEvent(response).result

            as XML;

            }

            // convert it to a string

            xmlString = response.toXMLString();

            // define the regex pattern to remove the namespaces from the string

            xmlnsPattern =

            new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");// remove the namespaces from the string representation of the XML

            namespaceRemovedXML = xmlString.replace(xmlnsPattern,

            "");

            // set the string rep. of the XML back to real XML

            responseXML =

            new XML(namespaceRemovedXML);

            return responseXML;

            }

             

            Finally my handler was working and putting the values in my dataGrid by changing it to:

             

            [Bindable]

            public var item:XMLList = null;

            private function searchHandler(event:ResultEvent):void{

            item=result(event).game;

            }