【问题标题】:XML - Get parent of selected nodeXML - 获取选定节点的父节点
【发布时间】:2012-02-16 15:52:25
【问题描述】:

我有一个项目,我根据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>

【问题讨论】:

  • 您的问题有点难以理解 - 我试图改进措辞。如果我误解了什么,请告诉我!
  • 我想你明白了 :) 非常感谢你的回答! :) 我马上去看看.. =)

标签: xml actionscript-3 apache-flex


【解决方案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();
}

它将在&lt;objects&gt; 下方的第一级上查找&lt;object&gt; 节点,其子树的某处包含查询项,然后仅将view001attribute 作为字符串返回。真的很简洁很短,你不觉得吗?

顺便说一句,这是假设您的完整 XML 文档有一个我不知道的根节点 - 您发布的 XML 无效,并且看起来像一个 sn-p。

【讨论】:

  • 非常感谢!你看,你发布的最后一段代码是我丢失它的地方。我现在明白了,你已经为我解释了,但自己弄清楚有点麻烦。我会看看我是否可以从中学习 =) 是的,我看到我的 XML 缺少结尾importInfo 的标签。 (我的 XML 中确实有它,但它只是没有显示在这里..)。
  • 一张便条。我需要该父节点的 XML,以便我可以读取所有树值(view001、view002 和 view003)。
  • 我读对了吗,如果我应该改用这个? (获取包含所有 3 个数据字段的 xml): public function getParentView ( fullTree : XML, queryItem:XML ) : String { return fullTree.对象.对象。 (descendants().contains(queryItem)); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-02
  • 2014-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多