문제

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> ?

도움이 되었습니까?

해결책

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);

다른 팁

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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top