3 Replies Latest reply on Mar 8, 2007 6:36 AM by eTJC

    HTTPService - How to databind an XML attribute

    eTJC
      I have a simple servlet that I call usign the HTTPService tag. Here is an example:

      <mx:HTTPService
      id="c4Test" method="POST" resultFormat="object" contentType="application/xml"
      url="myServlet"
      useProxy="false">
      <mx:request>

      I call my servlet from my Flex application (via the HTTPService tag). The "myServlet" takes the request and responds with an XML response, such as this:

      <myRoot>
      <title id="500">Hello World</title>
      </myRoot>

      I would like to display the attribute "id" from within the "title" tag in a label field. I attempt to do it like this but I get a syntax error:

      <mx:Label text="{c4Test.lastResult.myRoot.title[@message]}"/>

      I know the syntax is probably wrong, but how does one reference an attribute. Referencing the "title" element works fine using this syntax (but I don't want the title element, I want tithe id attribute value).

      <mx:Label text="{c4Test.lastResult.myRoot.title}"/>

      I am trying to avoid using ActionScript at this point....trying to do it with simple mx:controls

      Thanks for any help.



        • 1. Re: HTTPService - How to databind an XML attribute
          swebster
          Change the resultFormat of your service to "e4x" and you can then use the new E4X syntax built into ActionScript 3.0:

          <mx:HTTPService ... resultFormat="e4x" ... />

          <mx:Label text="{c4Test.lastResult.myRoot.title.@id}"/>

          Find out more about E4X here: http://livedocs.macromedia.com/flex/2/docs/00001912.html
          • 2. Re: HTTPService - How to databind an XML attribute
            eTJC Level 1
            Hmmm....doesn't seem to work for me. I changed my HTTPService tag to:

            <mx:HTTPService
            id="c4Test" method="POST" resultFormat="e4x" contentType="application/xml" url="{txtURL.text}" useProxy="false">

            And the label tag has this:

            <mx:Label text="{c4Test.lastResult.myRoot.title.@id}"/>

            I don't get any text in the label. As a test, I switched the label back to getting just the title element such as this:

            <mx:Label text="{c4Test.lastResult.myRoot.title}"/>

            And this did not work either...the label is empty. I had to switch the resultFormat back to "object" instead of "e4x" to get the title. I will read up more on e4x and actionscript but overall, can you bind a xml attribute result to a label text??

            Thanks!!
            • 3. Re: HTTPService - How to databind an XML attribute
              eTJC Level 1
              Ok...got it to work! It looks like when dealing with an e4x result set, you do not need to reference the root node. My xml result looked liked this:

              <myRoot>
              <title id="500">Hello World</title>
              </myRoot>

              I was attempting to reference the "id" attribute like this (which was not working:

              <mx:Label text="{c4Test.lastResult.myRoot.title.@id}" />

              But to get it to work, I did not need to inlcude the root "myRoot" node in the expression, like this:

              <mx:Label text="{c4Test.lastResult.title.@id}" />

              This worked.

              Thanks!