XML - 获取所选节点的父节点

时间:2012-02-16 15:52:26

标签: xml actionscript-3 flex

我有一个项目,我根据Tree的XML数据提供程序中的信息选择一个场景。

正如您在下面的XML中看到的,我有两个场景。每个属性都有view001属性,该属性仅存在于树层次结构中的最高级别。我需要做的是找出存储在这个属性中的信息。

我只需要查看包含树中当前所选项目的那个(名为treeView,dataProvider是xml文件,labelfield是.@name)。

我知道的一个问题是,我只是检查第一个parent()。我的问题是,我不知道如何“逐步”通过XML,一次一个父母。我想这也许是我唯一的问题..

这是我的功能:

public function getParentItem():XML {
    var selectedItem:XML = new XML(treeView.selectedItem);

    while(selectedItem.@view001.toString() == "") {
        try{
            selectedItem = treeView.selectedItem.parent();
        } catch(err:Error) {
            trace("Error: No parents found!");
            return null;
        }
    }
    return selectedItem;
}

这是我的XML文件:

<importInfo name="Main" cImageW="2880" cImageH="1620" dImageW="2880" dImageH="1620" bImageW="1280" bImageH="720" aspectRatio="16/9">
<objects name="ENI Goliat 360">
    <object name= "Area A" color="010101" length="1300.54" width="1300.54" height="1300.54" posx="-140.48" posy="-259.226" posz="57.6476" view001="Template_View001" view002="Template_View002" view003="Template_View003">
        <object name= "Template" color="010111" length="910.45" width="1308.06" height="360.91" posx="-197.153" posy="-119.162" posz="-67.1688" >
            <object name= "Vacum Lid" color="010121" length="-1000.54" width="-732.224" height="14.029" posx="375.825" posy="367.99" posz="-60.4437" LastPose =" undefined" />
            <object name= "Xmas Tree 1" color="010131" length="209.81" width="185.618" height="176.285" posx="-140.48" posy="-259.226" posz="57.6476" LastPose =" undefined" >
                <object name= "Control Module 1" color="010141" length="33.8371" width="33.8371" height="71.277" posx="-210.844" posy="-274.111" posz="49.6147" LastPose =" undefined" />
            </object> 
            <object name= "Xmas Tree 2" color="010151" length="209.81" width="185.618" height="176.285" posx="157.511" posy="-259.226" posz="57.6476" LastPose =" undefined" >
                <object name= "Control Module 2" color="010161" length="33.8371" width="33.8371" height="71.277" posx="87.1474" posy="-274.111" posz="49.6147" LastPose =" undefined" />
            </object> 
            <object name= "Xmas Tree 3" color="010171" length="-209.81" width="-185.618" height="176.285" posx="-139.859" posy="300.043" posz="57.6476" LastPose =" undefined" >
                <object name= "Control Module 3" color="010181" length="-33.837" width="-33.8371" height="71.277" posx="-69.4951" posy="314.927" posz="49.6147" LastPose =" undefined" />
            </object> 
            <object name= "Xmas Tree 4" color="010191" length="-209.81" width="-185.618" height="176.285" posx="157.678" posy="300.043" posz="57.6476" LastPose =" undefined" >
                <object name= "Control Module 4" color="0101a1" length="-33.837" width="-33.8371" height="71.277" posx="228.042" posy="314.927" posz="49.6147" LastPose =" undefined" />
            </object> 
            <object name= "Hatches" color="0d0dd9" length="853.092" width="641.077" height="38.6531" posx="8.64197" posy="20.1918" posz="166.54" LastPose =" undefined" />
            <object name= "Manifold" color="0101c1" length="348.443" width="1055.62" height="256.673" posx="8.81242" posy="19.8837" posz="-47.4717" LastPose =" undefined" >
                <object name= "Umbilical" color="0101d1" length="15.6839" width="-1067.38" height="30.0319" posx="160.286" posy="-42.1586" posz="-14.7542" LastPose =" undefined" />
                <object name= "SAM Hatch" color="0101e1" length="104.111" width="162.785" height="23.9263" posx="8.81239" posy="6.30106" posz="186.356" LastPose =" undefined" />
                <object name= "SAM Hatch" color="0101f1" length="-104.111" width="-162.785" height="23.9263" posx="8.76724" posy="33.4806" posz="186.356" LastPose =" undefined" />
                <object name= "Manifold Cover" color="011101" length="208.662" width="639.95" height="0.393707" posx="8.81241" posy="19.8837" posz="-47.4717" LastPose =" undefined" />
                <object name= "Sealine Cover" color="011111" length="-228.002" width="1206.52" height="235.54" posx="-352.163" posy="19.8837" posz="124.118" />
            </object> 
        </object> 
        <object name= "10' Oil Eeas(SP06) TEE - TEMPLATE C" color="011121" length="33.8898" width="1473.6" height="89.1353" posx="-1924.2" posy="1175.54" posz="2402.62" />
        <object name= "10' Oil West(SP05) TEE - TEMPLATE C" color="011131" length="18.9734" width="1373.77" height="80.3972" posx="-1933.05" posy="624.902" posz="2402.62" />
        <object name= "6' Gas(GL02) TEMPLATE B - TEMPLATE C" color="011141" length="1350.58" width="2542.36" height="144.254" posx="14156.9" posy="19699.3" posz="2401.68" />
        <object name= "Umbilical(UM05) TEMPLATE B - TEMPLATE C" color="011151" length="1313.2" width="2942.5" height="137.481" posx="13516.9" posy="18206.4" posz="2402.62" />
        <object name= "Umbilical(UM05) TEMPLATE C - TEMPLATE I" color="011161" length="55.0352" width="1288.32" height="64.1479" posx="-50077.3" posy="-60757.5" posz="2402.62" />
        <object name= "6' Gas(GL03) TEMPLATE C - TEMPLATE D" color="011171" length="-10.2617" width="1537.04" height="85.0139" posx="-33206.3" posy="-34778.7" posz="2402.62" />
    </object>
    <object name="Goliat Sevan" color="57e157" length="4912.53" width="4912.53" height="4912.53" posx="-119.385" posy="-2970.76" posz="403.785" view001="Topside_View001" view002="" view003="" >
        <object name="Boom" color="640404" length="-651.017" width="1611.94" height="3641.64" posx="-169.37" posy="-2888.7" posz="3569.63" />
        <object name="Production" color="8c0606" length="-3499.51" width="5256.08" height="1946.97" posx="875.004" posy="-1031.51" posz="1702.66" />
        <object name="Lifeboats" color="780505" length="564.514" width="-3529.38" height="819.231" posx="-1337.81" posy="-4437.18" posz="1629.08" LastPose ="undefined" />
        <object name="Living Quarter" color="a00707" length="-1644.94" width="2099.55" height="1663.29" posx="-1716.48" posy="-3341.02" posz="1838.31" />
        <object name="Cranes" color="3c0303" length="-6720.73" width="1488.25" height="1516.48" posx="-561.582" posy="-4616.26" posz="2068.74" />
        <object name="Hull" color="280101" length="-3784.79" width="5998.56" height="2413.14" posx="-119.385" posy="-2970.76" posz="403.785" />
        <object name="Knudsen Shipping" color="b40808" length="-10560.2" width="100.002" height="2250.03" posx="2164.21" posy="-17398.6" posz="-493.164" LastPose ="undefined" />
    </object> 
</objects>

1 个答案:

答案 0 :(得分:1)

在找到@view001的值之前,一直向上工作是一种方法,但是你的while循环不起作用:@view001不会返回空字符串,如果它不存在,您必须选择当前项目的parent()以实际在层次结构中向上移动。

while (selectedItem.parent() != null && (selectedItem.@view == null || selectedItem.@view.length() == 0)) {
    selectedItem = selectedItem.parent();  
}

这应该这样做。但是,这种方法看起来很麻烦且无效 - 你应该使用E4X的强大功能来做到这一点!

这将从顶部遍历整个树:

public function getParentView ( fullTree : XML, queryItem:XML ) : String {
    return fullTree.
            objects.object.
                (descendants().contains(queryItem)).
                    @view001.toString();
}

它将在<object>下面的第一级查找<objects>节点,其子树某处包含查询项,然后仅返回view001属性作为字符串。真的很整洁,你不觉得吗?

BTW这假设您的完整XML文档有一个我不知道的根节点 - 您发布的XML无效,看起来像一个片段。