2

Consider this XML:

<root>
    <node>
        <subNode>123</subNode>
        <anotherNode>abc</anotherNode>
    </node>
    <node>
        <anotherNode>abc</anotherNode>
    </node>
</root>

This works, because E4X only finds 1 match, and returns an XML instead of an XMLList:

trace(myXml.node.subNode); // 123

But why this throws an Error #1065: Variable subNode is not defined?

trace(myXml.node.(subNode == 123).anotherNode);

Why doesn't it trace <anotherNode>abc</anothernode> ?

Pier
  • 10,298
  • 17
  • 67
  • 113

2 Answers2

1

This doesn't work because the player tries to find subNode in each node, and it can’t, so a ReferenceError exception is thrown.

In this case you can use hasOwnProperty method to ensure that the property exists:

trace(myXml.node.(hasOwnProperty("subNode") && subNode == 123).anotherNode);
null.point3r
  • 1,053
  • 2
  • 9
  • 17
0

I have test it, it seems the second node hasn't the subNode. So try to add subNode(though I think there should be another way to solve this).

<root>
   <node>
       <subNode>123</subNode>
       <anotherNode>abc</anotherNode>
    </node>
    <node>
        <subNode>321</subNode>
        <anotherNode>abc</anotherNode>
    </node>
</root>
Pan
  • 2,101
  • 3
  • 13
  • 17
  • It's obvious the second node hasn't got a subNode, that's exactly my point. I know it doesn't work because E4X only finds one result, and in consequence it returns an XML instead of XMLList. My question is why that affects the filtering capacities of E4X. – Pier Jun 25 '13 at 16:47