3 Replies Latest reply on Jun 7, 2006 3:46 AM by Joe_Casey

    More XML troubles

    Joe_Casey
      Flex XML capabilities seem very exciting but I have to say that getting it to work has been murder. This example is from "Flex 2.0 Developer's Guide > Data Access and Interconnectivity > Binding Data > Binding data with curly braces "

      It compiles and runs but the output is shown in XML format - instead of "Big Screen Television" you get "<description>Big Screen Television</description>" The comments say that toString() is called by default, but even calling it explicitly doesn't solve the problem.

      Ideas?

      <?xml version="1.0"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" width="600" height="900">

      <mx:Script>
      <![CDATA[
      [Bindable]
      public var xdata:XML = <order>
      <item id = "3456">
      <description>Big Screen Television</description>
      <price>1299.99</price><quantity>1</quantity>
      </item>
      <item id = "56789">
      <description>DVD Player</description>
      <price>399.99</price>
      <quantity>1</quantity>
      </item>
      </order>;

      ]]>
      </mx:Script>

      <mx:Label text="Using .. notation."/>
      <!-- Inline databinding will automatically call the toString() when the binding destination is a string. -->
      <mx:Text width="100%" text="{xdata..description}"/>

      <mx:Label text="Using . notation."/>
      <mx:Text width="100%" text="{xdata.item.description}"/>


      <mx:Label text="Using || (or) notation."/>
      <mx:Text width="100%" text="{xdata.item.(@id=='3456'||@id=='56789').description}"/>
      </mx:Application>
        • 1. Re: More XML troubles
          Level 7
          Yes, I just noticed this recently in the docs myself. At first I thought it
          was a doc bug, but then I thought, well, maybe they did it this way on purpose
          -- maybe they were trying to simply show you that an E4X expression like
          "xdata..description" could be put into the curly braces.

          But that is misleading, because xdata..description evaluates two two nodes,
          not one, and it is bound to a Text control, so, since a Text control only
          wants one element, it just calls toString() on the result; and when you call
          toString() on an XMLList, if that list contains more than one element, then
          the result looks like XML.

          To make this code actually USEFUL, instead of seeing some silly XML onscreen,
          you should change the <mx:Text> tag to an <mx:List> tag. *Then* you will see
          the cool stuff happen. xdata..description evaluates to an XMLList, and the
          List control says, okay fine, I can handle that, I'll display one result per line.

          Since you tripped over this just like I did, I think I will file it as a doc bug.

          Thanks,

          --
          Mike Morearty
          Developer, Flex Builder team
          http://www.morearty.com/blog


          Joe Casey wrote:
          > Flex XML capabilities seem very exciting but I have to say that getting it to
          > work has been murder. This example is from "Flex 2.0 Developer's Guide > Data
          > Access and Interconnectivity > Binding Data > Binding data with curly braces "
          >
          > It compiles and runs but the output is shown in XML format - instead of "Big
          > Screen Television" you get "<description>Big Screen Television</description>"
          > The comments say that toString() is called by default, but even calling it
          > explicitly doesn't solve the problem.
          >
          > Ideas?
          >
          > <?xml version="1.0"?>
          > <mx:Application xmlns:mx="<a target=_blank class=ftalternatingbarlinklarge
          > href=" http://www.adobe.com/2006/mxml"">http://www.adobe.com/2006/mxml"</a>
          > width="600" height="900">
          >
          > <mx:Script>
          > <![CDATA[
          > [Bindable]
          > public var xdata:XML = <order>
          > <item id = "3456">
          > <description>Big Screen Television</description>
          > <price>1299.99</price><quantity>1</quantity>
          > </item>
          > <item id = "56789">
          > <description>DVD Player</description>
          > <price>399.99</price>
          > <quantity>1</quantity>
          > </item>
          > </order>;
          >
          > ]]>
          > </mx:Script>
          >
          > <mx:Label text="Using .. notation."/>
          > <!-- Inline databinding will automatically call the toString() when the
          > binding destination is a string. -->
          > <mx:Text width="100%" text="{xdata..description}"/>
          >
          > <mx:Label text="Using . notation."/>
          > <mx:Text width="100%" text="{xdata.item.description}"/>
          >
          >
          > <mx:Label text="Using || (or) notation."/>
          > <mx:Text width="100%"
          > text="{xdata.item.(@id=='3456'||@id=='56789').description}"/>
          > </mx:Application>
          >
          • 2. Re: More XML troubles
            Joe_Casey Level 1
            Thanks, Mike, that works. (You have to also change "text=" to "dataProvider=", but you knew that. )

            I have more XML problems, I'll be back later.
            • 3. Re: More XML troubles
              Joe_Casey Level 1
              I just wanted to remind anyone reading this that this in another example of the problems created by the "feature" of allowing XMLList objects to be treated like XML objects if they contain exactly one element, and otherwise to be treated differently.

              I'll bet this example worked perfectly at first, with one 'item' contained in the xdata variable. Then to make it seem more realistic they added a second item, changing the data but not the code, and it broke. Programmer beware.