2 Replies Latest reply on Sep 27, 2006 4:59 PM by mike_morearty

    XML access with namespaces?

    derRaab(); Level 1
      Hi!

      I started learning Flex by using Flex but now I have the first problem I can't solve. I think this will be only a basic problem but in other forums nobody could help me. And of course I didn't find anything about this in the help. :)

      In the following MXML-Code I tried to use the flickr SOAP API which works, but I don't get the real usage of the new XML class. As you can see in the function called "recieveFlickrResult" I'm not able to access the attribute I want to. The syntax seems to be correct in my eyes because, if I create a new XML-Object using the XMLString I can access the attribute. I also tried several other methods like attributes(), children() and so on. I really don't get it.

      I think I'm making a fundamental mistake! Maybe the way I'm using the access through namespaces (function "extractFlickrResponse") is wrong?

      Maybe you want to try this MXML-File:


      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">

      <mx:HTTPService id="flickrService" url=" http://api.flickr.com/services/soap/" useProxy="false" contentType="application/xml" resultFormat="xml"></mx:HTTPService>


      <mx:Script>
      <![CDATA[

      public var apiURL:String = " http://api.flickr.com/services/soap/";

      public var api_key:String = "0cdbd0b93883f563e3bea67611e44c83";
      public var nsid:String;


      public var urlRequest:URLRequest;
      public var resultLoader:URLLoader;

      public function debug(s:String):void{
      debugTxt.text += s + "\r";
      }
      var sNS:Namespace = new Namespace(" http://www.w3.org/2003/05/soap-envelope");
      var xNS:Namespace = new Namespace("urn:flickr");

      public function generateSOAPRequestXML(method:String, params:Object):XML{

      var requestXML:XML = <s:Envelope
      xmlns:s=" http://www.w3.org/2003/05/soap-envelope"
      xmlns:xsi=" http://www.w3.org/1999/XMLSchema-instance"
      xmlns:xsd=" http://www.w3.org/1999/XMLSchema"
      >
      <s:Body>
      <x:FlickrRequest xmlns:x="urn:flickr">
      <method>{method}</method>
      <api_key>{api_key}</api_key>
      </x:FlickrRequest>
      </s:Body>
      </s:Envelope>

      var flickrXMLList:XMLList = requestXML.sNS::Body.xNS::FlickrRequest;

      for (var param:String in params){
      var newparamNode:XML = <{param}>{params[param]}</{param}>
      flickrXMLList.appendChild(newparamNode);
      }

      return requestXML;
      }

      public function extractFlickrResponse(response:XML):XMLList{
      return response.sNS::Body.xNS::FlickrResponse;
      }


      public function startFlickrRequest(requestXML:XML):void{

      urlRequest = new URLRequest(apiURL);
      urlRequest.contentType = "text/xml";
      urlRequest.method = URLRequestMethod.POST;
      urlRequest.data = requestXML;

      resultLoader = new URLLoader(urlRequest);
      resultLoader.addEventListener("complete", recieveFlickrResult);

      }
      public function recieveFlickrResult(evt:Event):void{
      if (evt.type == "complete"){
      var resultXML:XML = new XML(resultLoader.data);
      debug("resultXML :\r" + resultXML);
      var response:XMLList = extractFlickrResponse(resultXML);
      debug("");
      debug("response :\r" + response);

      debug("response.@nsid : " + response.@nsid); // returns nothing!

      var newXML:XML = new XML(response.toString());
      debug("");
      debug("newXML :\r" + newXML);
      debug("");
      debug("newXML.@nsid : " + newXML.@nsid); //returns the value
      };
      }


      public function init():void{

      var nsidRequestXML:XML = generateSOAPRequestXML('flickr.people.findByUsername', {username :"der_raab"});

      startFlickrRequest(nsidRequestXML);

      }


      ]]>
      </mx:Script>
      <mx:TextArea id="debugTxt" text="" left="10" right="10" bottom="10" top="10"/>

      </mx:Application>