سؤال

لدي XML مع الهيكل التالي، على سبيل المثال giveacodicetagpre.

جميع الأطفال لديهم اسم "العقدة".ما أحتاج إليه هو الحصول على XMLList (أو XML، بغض النظر)، مع نفس التسلسل الهرمي، ولكن يحتوي على العقد فقط مع العلم "صحيح".

النتيجة التي أحتاج إليها على سبيل المثال: giveacodicetagpre.

هل هناك أي طريقة لطيفة للقيام بذلك باستخدام E4X (دون التكرار من خلال حلقة)؟حاولت القيام بما يلي: XML.Node. (@ العلم=="صحيح")، ولكن النتيجة في هذه الحالة هي: giveacodicetagpre.

أي أفكار؟شكرا لك!

هل كانت مفيدة؟

المحلول

Here a one liner in e4x as you ask :

xml..node.((@flag=="false") && (delete parent().children()[valueOf().childIndex()]))

it delete the node to the current XML so pay attention to have a copy of your current XML.

By the way you should know that e4x just do a loop under the hood, and that one liner will not be faster than a custom loop.

var xml:XML=<root>
    <node id="1" flag="false"/>
    <node id="2" flag="true"/>
    <node id="3" flag="false"/>
    <node id="4" flag="false"/>
    <node id="5" flag="true">
        <node id="5.1" flag="false"/>
        <node id="5.2" flag="true"/>
        <node id="5.3" flag="false"/>
        <node id="5.4" flag="true"/>
    </node>
    <node id="6" flag="true"/>
    <node id="7" flag="false">
        <node id="7.1" flag="false"/>
        <node id="7.2" flag="true"/>
        <node id="7.3" flag="false"/>
        <node id="7.4" flag="true"/>
    </node>
    <node id="8" flag="false"/>
</root>

trace("-- before --")
trace(xml.toXMLString())

xml..node.((@flag=="false") && (delete parent().children()[valueOf().childIndex()])) 

trace("\n-- after --")
trace(xml.toXMLString())

نصائح أخرى

It didn't kill those nodes, cause your condition xml.node.(@flag="true") works only on the direct children of root, you have to make another one for the children of node

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top