0 Replies Latest reply on Feb 24, 2010 11:18 PM by jake_flex

    E4X: What happens to the root element?

    jake_flex Level 2

      I'm working with XML using the E4X notation. I'm used to the "old school" XML APIs that use DOM and I'm a bit confused.

       

      For example, let's consider the following XML

       

      <mx:XML id="myXML" >
        <top>
          <sub1 id="uno">
            <sub2>hello</sub2>
            <sub3>world</sub3>
          </sub1>
          <sub1 id="duo">
            <sub2>ok</sub2>
            <sub3>ko</sub3>
          </sub1>
        </top>
      </mx:XML>
      
      

       

      In my code I use the following traces. This is done with the De MonsterDebugger, but all others should work the same way. I have casted the traces to String only to make it easier to ask this question (no need post images)

       

      MonsterDebugger.trace(this, "myXML:" + myXML);
      MonsterDebugger.trace(this, "myXML.sub1: " + myXML.sub1);
      MonsterDebugger.trace(this, "myXML.sub1.sub2: " + myXML.sub1.sub2);
      

       

      What I get as output is

       

      (String) = myXML:<top>
        <sub1 id="uno">
          <sub2>hello</sub2>
          <sub3>world</sub3>
        </sub1>
        <sub1 id="duo">
          <sub2>ok</sub2>
          <sub3>ko</sub3>
        </sub1>
      </top>
      

       

      (String) = myXML.sub1: <sub1 id="uno">
        <sub2>hello</sub2>
        <sub3>world</sub3>
      </sub1>
      <sub1 id="duo">
        <sub2>ok</sub2>
        <sub3>ko</sub3>
      </sub1>
      

       

      (String) = myXML.sub1.sub2: <sub2>hello</sub2>
      <sub2>ok</sub2>
      

       

      All is fine above and this was a bit long abstract for my question. The question is what happens to the root element, in this case <top>? I was trying to access the data with the following notation

       

      myXML.top.sub1; // etc
      

       

      I spent quite a lot of time trying to get it to work, until I discovered that the root element is not used. Can anyone explain this?

       

      Or, I guess the simple explanation is that the root node is ignored and the sub nodes are created as properties of the object. The question also could be stated as: Why isn't this documented in the Flex API Reference?

       

      Thanks.

       

      P.S. I found this article on common E4X pitfalls which also has other interesting topics when working with E4X.