3 Replies Latest reply on May 17, 2007 12:32 PM by RekhaPatel

    Parsing xml

    abdel.olakara Level 1
      Hi everybody,
      I am trying to parse a atom feed and display the content using a flex application. I having trouble in parsing the http response. I get the feed using the HTTPService and I am using XML Object to parse the xml response. Below i have given the simple code that i tried testing with:

      <?xml version="1.0" ?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="loadData();" >
      <mx:HTTPService id="atomUrl" url=" http://weblogs.macromedia.com/flexteam/atom.xml" result="httpResult(event);" />

      <mx:Script>
      <![CDATA[

      import mx.controls.Alert;
      private var xmlDoc: XML;
      private var tempString: String;
      private var itemXMLlist: XMLList;

      private function loadData() : void {
      atomUrl.send();
      }

      private function httpResult(event:Event):void {
      tempString = String(atomUrl.lastResult.feed.title);
      titleT.text=tempString;

      xmlDoc = XML(atomUrl.lastResult);

      textArea.text = xmlDoc.toString();

      }
      ]]>
      </mx:Script>

      <mx:Panel title="Content" width="100%" height="100%">
      <mx:TextInput id="titleT" />
      <mx:TextArea id="textArea" width="100%" height="100%"/>
      </mx:Panel>
      </mx:Application>

      I also tried using processingInstructions method and get XMLList. But it always is 0.
      Thanks for your help,
      -- Abdel Olakara
      http://olakara.googlepages.com
      http://technopaper.blogspot.com
        • 1. Re: Parsing xml
          mike_morearty Level 1
          Hi Abdel,

          Some of the problems here are addressed by a recent post on my blog: http://www.morearty.com/blog/2007/03/13/common-e4x-pitfalls/

          (1) You need resultFormat="e4x" on the HTTPService tag
          (2) You need atomUrl.lastResult.title, not atomUrl.lastResult.feed.title, because "feed" is the top-level tag.

          But there is a third issue not addressed in that post, and this is tricky: XML namespaces. I'm not an expert on it (but I probably know more about it than most people do at this point). Basically, as the code is currently written, atomUrl.lastResult.title refers to the <title> tag in the unnamed namespace, but as you can see in the incoming document, the <title> tag is actually part of the " http://purl.org/atom/ns#" namespace. So to fix it, add this line of code at the top of your httpResult() function, before you start parsing the XML that was returned:

          default xml namespace = " http://purl.org/atom/ns#";
          • 2. Re: Parsing xml
            abdel.olakara Level 1
            Hi mike,
            Thanks for the help. I will modify my code and check out if the problem gets solved..
            Thanks,
            -- Abdel Olakara
            http://olakara.googlepages.com
            http://technopaper.blogspot.com
            • 3. Re: Parsing xml
              RekhaPatel
              Thanks mike. Your suggestion worked for me.

              Here is my use case for the benefit of other developers.

              This is what I was getting back in my response and I needed to parse the XML to get the Timestamp value :
              <GeteBayOfficialTimeResponse xmlns="urn:ebay:apis:eBLBaseComponents">
              <Timestamp>2007-05-17T19:25:44.038Z</Timestamp>
              <Ack>Success</Ack>
              <Version>513</Version>
              <Build>e513_core_Bundled_4663915_R1</Build>
              </GeteBayOfficialTimeResponse>

              My function:
              private function onComplete(event:Event):void {
              default xml namespace = "urn:ebay:apis:eBLBaseComponents";
              var response:XML;
              var loader:URLLoader = URLLoader(event.target);
              response = new XML(loader.data);

              trace(response.Timestamp);
              }

              till I added
              default xml namespace = "urn:ebay:apis:eBLBaseComponents";

              trace(response.Timestamp);
              would return nothing

              As soon as I added, it worked like a charm!

              Thanks again Mike.