6 Replies Latest reply on Sep 27, 2006 9:28 PM by mike_morearty

    e4x bug

    Smitchmor Level 1
      Please see below for example:

      When the folloiwng line is uncommented
      //trace(myXML.item.(@quantity2 == "5"));
      the following error is raised.
      ReferenceError: Error #1065: Variable @quantity2 is not defined.

      Anyone know if this is a bug, or part of the e4x specification? Also, if it's the former, does
      anyone know where to find a list of active Flex 2.0 bugs. Thanks.

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute"
      var myXML:XML;

      public function initApp():void {
      myXML =
      <item id="1" quantity="45" quantity2="5">
      <menuName type="">burger</menuName>
      <item id="2" quantity="40">
      <menuName type="dinner">fries</menuName>
      trace(myXML.item.(@quantity == "45"));
      //trace(myXML.item.(@quantity2 == "5"));
        • 1. Re: e4x bug
          ntsiii Level 3
          While I am still new to e4x, that code looks fine. I have not run it because I am doing 1.5 with FP 7 at the moment.

          Have you determined any more info? Can you get at other data in the xml document?

          What are you theorizing when you use the words "uneven" attributes?

          • 2. Re: e4x bug
            Smitchmor Level 1
            The code looks fine with me too. But notice that the following line is commented
            //trace(myXML.item.(@quantity2 == "5"));
            If it is uncommented, the code will error.

            By uneven I mean, quantity2 is not an attribute in both elements, whereas quantity1 is.
            • 3. e4x bug
              This behaviour is apparently by design - the relevant part of the documentation is:

              Programming ActionScript 3.0 > Core ActionScript 3.0 Data Types and Classes > Working with XML

              .. in which it says...


              If you try to filter on attributes that do not exist, Flash Player will throw an exception. For example, the following generates an error (in trying to execute the second to last line) because there is no id attribute in the second p property:

              var doc:XML =
              <p id='foo'>Hello</p>
              var matchingNodes:XMLList = doc.p.(@id == 'foo');

              Instead, identify those properties that have the matching attribute prior to filtering, as in the following code:

              var doc:XML =
              <p id='foo'>Hello</p>
              var hasIDAttribute:XMLList = new XMLList();
              for (var i:uint = 0; i < doc.p.length(); i ++) {
              if (doc.p .attribute("id") != undefined) {
              hasIDAttribute += doc.p

              • 4. e4x bug
                Ed_Syrett Level 1
                I'm somewhat disappointed that the e4x doesn't cope with this at all, so after writing the previous post, I decided to have a go at a solution. So here's a generic filter function:

                private function filterNodes(source:XMLList, attributeName:String):XML
                var filterList:XML = new XML("<filteredList/>");

                for (var x:uint = 0; x < source.length(); x++)
                if (source[x].attribute(attributeName) != undefined)

                return filterList;

                which means that you can write this:

                trace(filterNodes(myXML.item, "quantity2").item.(@quantity2=="5"));

                Apologies if this is a bit late, but I though I'd better sort this out for myself too as I suspect I'm going to have this problem as well in future.....

                • 5. Re: e4x bug
                  mike_morearty Level 1
                  I agree with you, it's disappointing that E4X doesn't handle this. Here is another solution that I saw once -- call hasOwnProperty() before trying to access the property:

                  trace(myXML.item.(hasOwnProperty("@quantity2") && @quantity2 == "5"));

                  Clunky, but probably more efficient than building a new temporary XML object.
                  • 6. Re: e4x bug
                    mike_morearty Level 1
                    Actually here's another way, even cleaner (still not quite as good as if E4X allowed you to reference properties that aren't defined, but pretty close) -- call the attribute() function, with attribute("attrname") instead of @attrname:

                    trace(myXML.item.(attribute("quantity2") == "5"));