Skip navigation

This works (outputs ID’s):

new Array();
var attributes:XMLList = _data.Attributes..AttributeDescriptor;
for each(var attribute:XML in attributes)
{
	trace(attribute.@ID);
}

This doesn’t (outputs nulls):

			
var attributes:XMLList = _data.Attributes..AttributeDescriptor;
for each(var attribute:XML in attributes)
{
	trace(attribute.@ID);
}

Any questions?

Update: Kudos to Larry (developmentastic.com) for beating his head against this brick wall on the Winestore project.

About these ads

2 Comments

  1. I tried to replicate your example, but could not find any problem. Could you post the xml you are using?

    Greetz Erik

  2. Erik,

    The context is a Flash (not Flex) PureMVC Kiosk project. We’re retrieving the XML from a Web Service in a [PureMVC] proxy. When the SOAP response comes back from the service, it is handled by a handleRetrieveResponse(response:XML) method in the proxy. We’re trying to parse the XML as follows in that method:

    // E4X hack / fix
    new Array();
    var wines:XMLList = response..WineDescriptor;
    var descriptors:Array = new Array();
    for each(var wine:XML in wines)
    {
        descriptors.push(WineDescriptor.parseXml(wine));
    }

    // Send descriptors array in a notification.

    The XML in the response has the following structure:

    <?xml version=”1.0″ encoding=”utf-8″?>
    <soap:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/”>
      <soap:Body>
        <FindResponse xmlns=”http://www.winestore.com/oaky/1.0/products”>
          <FindResult>
            <WineDescriptor ID=”int” Relevancy=”int” Barcode=”string” UpdatedAt=”dateTime” Organic=”boolean” FeaturedItem=”boolean” IsOnTastingMachine=”boolean” BinLocation=”string” WebItem=”boolean” StockQuantity=”double” Department=”string” Price=”decimal” ImageName=”string” RatingsScore=”int” RatingsCount=”int”>
              <Name>string</Name>
              <Origin>string</Origin>
              <Varietal>string</Varietal>
              <Vintner>string</Vintner>
              <Vintage>string</Vintage>
              <Color>string</Color>
              <Body>string</Body>
              <Description>string</Description>
              <Attributes>
                <AttributeDescriptor xsi:nil=”true” />
                <AttributeDescriptor xsi:nil=”true” />
              </Attributes>
              <Pairings>
                <PairingDescriptor xsi:nil=”true” />
                <PairingDescriptor xsi:nil=”true” />
              </Pairings>
              <Occasions>
                <OccasionDescriptor xsi:nil=”true” />
                <OccasionDescriptor xsi:nil=”true” />
              </Occasions>
              <LabelRectangle>
                <Location xsi:nil=”true” />
                <Size xsi:nil=”true” />
                <X>int</X>
                <Y>int</Y>
                <Width>int</Width>
                <Height>int</Height>
              </LabelRectangle>
            </WineDescriptor>

            …

    One unique thing about this is that we need to set the default namespace to “http://www.winestore.com/oaky/1.0/products” as you can see in the SOAP response. At the top of the class we declare:

    default xml namespace = “http://www.winestore.com/oaky/1.0/products”;

    If you don’t do that, your E4X doesn’t return anything at all.

    It simply appears that instantiating an Array before you perform any E4X work initializes something that allows it to work in this context. If you remove “new Array();” (which of course, does nothing), you won’t be able to read anything via E4X.


One Trackback/Pingback

  1. By Recent Links Tagged With "e4x" - JabberTags on 01 Dec 2008 at 5:04 am

    […] Dfsut on Wed 12-11-2008 Flex in a week and AS3 workshop Saved by mjohnson16 on Mon 10-11-2008 Funny little bug in Flash E4X Saved by necosan on Mon 10-11-2008 5+ hours of free Flex training videos now onlineā€¦ Saved by […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: