2012-02-20 41 views
0

我有以下XML聲明:柔性XML children()方法奇怪的行爲

public var reqData:XML = <root> 
    <Requirement ID="REQ-GEN-0.1" title="exigence gen 1" description="blabla 01" testable="true"/> 
    <RequirementSet ID="GUI REQ"> 
    <Requirement ID="REQ-GUI-1.1" title="exigence ihm 1" description="blabla 11" testable="true"/> 
    <Requirement ID="REQ-GUI-1.2" title="exigence ihm 2" description="blabla 12" testable="false"/> 
    </RequirementSet> 
    <RequirementSet ID="PERF REQ"> 
    <Requirement ID="REQ-PERF-2.1" title="exigence perf 1" description="blabla 21" testable="true"/> 
    <Requirement ID="REQ-PERF-2.2" title="exigence perf 2" description="blabla 22" testable="false"/> 
    <Requirement ID="REQ-PERF-2.3" title="exigence perf 3" description="blabla 23" testable="true"/> 
    <Requirement ID="REQ-PERF-2.4" title="exigence perf 4" description="blabla 24" testable="false"/> 
    <Requirement ID="REQ-PERF-2.5" title="exigence perf 5" description="blabla 25" testable="false"/> 
    <Requirement ID="REQ-PERF-2.6" title="exigence perf 6" description="blabla 26" testable="false"/> 
    </RequirementSet> 
    <RequirementSet ID="BUS REQ"> 
    <RequirementSet ID="BUS 1 REQ"> 
     <Requirement ID="REQ-BUS-3.1.1" title="exigence bus 1" description="blabla 311" testable="false"/> 
     <Requirement ID="REQ-BUS-3.1.2" title="exigence bus 2" description="blabla 312" testable="true"/> 
    </RequirementSet> 
    <RequirementSet ID="BUS 2 REQ"> 
     <Requirement ID="REQ-BUS-3.2.1" title="exigence bus3" description="blabla 321" testable="true"/> 
    </RequirementSet> 
    <RequirementSet ID="BUS 3 REQ"/> 
    </RequirementSet> 
</root>; 

我居住的先進數據網格與該XML,但問題是,沒有檢測要求:<Requirement ID="REQ-BUS-3.2.1" title="exigence bus3" description="blabla 321" testable="true"/>

我已經重寫了HierarchicalData類的方法,看看有什麼happenned:

override public function canHaveChildren(node:Object):Boolean 
     { 
      if (node is XML && node != null){ 
       var xmlNode:XML = node as XML; 
       trace("node:"+node); 
       trace("node.children:"+node.children()); 
       trace("xmlNode.name:"+xmlNode.name()); 
       trace("xmlNode.localName:"+xmlNode.localName()); 
       trace("xmlNode.attributes:"+xmlNode.attributes()); 
       trace("xmlNode.attributes:"+xmlNode.nodeKind()); 
       trace("xmlNode.children():"+xmlNode.children()); 
       trace("xmlNode.children().length():"+xmlNode.children().length()); 

       if(xmlNode.children().length()>0){ 
        var xmlNodeChildren:XMLList = xmlNode.children() as XMLList; 
        var xmlNodeFirstChild:XML = xmlNodeChildren[0]; 
        trace("xmlNodeFirstChild:"+xmlNodeFirstChild); 
        trace("xmlNodeFirstChild.name():"+xmlNodeFirstChild.name()); 
        trace("xmlNodeFirstChild.comments():"+xmlNodeFirstChild.comments()); 
        trace("xmlNodeFirstChild.attributes():"+xmlNodeFirstChild.attributes()); 
        trace("xmlNodeFirstChild.nodeKind():"+xmlNodeFirstChild.nodeKind()); 
        trace("xmlNodeFirstChild.descendants():"+xmlNodeFirstChild.descendants()); 
       } 


      } 

這裏是結果(控制檯),我只是不明白,在所有:

node:<RequirementSet ID="BUS 2 REQ"> 
    <Requirement ID="REQ-BUS-3.2.1" title="exigence bus3" description="blabla 321" testable="true"/> 
</RequirementSet> 
node.children: 
xmlNode.name:RequirementSet 
xmlNode.localName:RequirementSet 
xmlNode.attributes:BUS 2 REQ 
xmlNode.attributes:element 
xmlNode.children(): 
xmlNode.children().length():1 
xmlNodeFirstChild: 
xmlNodeFirstChild.name():Requirement 
xmlNodeFirstChild.comments(): 
xmlNodeFirstChild.attributes():REQ-BUS-3.2.1exigence bus3blabla 321true 
xmlNodeFirstChild.nodeKind():element 
xmlNodeFirstChild.descendants(): 

該節點沒有被children()方法檢測到,但它存在。問題是我無法看到XML.abc的源代碼,因爲它不是開源的。有人能告訴我發生了什麼事嗎?這是一個錯誤還是其他的東西?

回答

1

的節點由children()方法只是你希望的方式回升,因爲否則的children()length()將爲0你要明白那是什麼trace(node.children());實際上意味着trace (XMLList(node.children()).toString()); - 在一個XMLList您呼叫的toString() - 其輸出可以有許多不同的結果,但顯然不是很容易預測的:

var xml : XML = <node><child>MyText</child></node>; 
trace (xml.children()); 
// => MyText 

xml = <node><child value="MyText" /></node>; 
trace (xml.children()); 
// => nothing 

xml = <node><child key="myKey" id="1" value="MyText" /></node>; 
trace (xml.children()); 
// => nothing 

xml = <node><child id="1" key="text">MyText</child></node>; 
trace (xml.children()); 
// => MyText 

xml = <node><child id="1">MyText<child id="2">MyOtherText</child></child></node>; 
trace (xml.children()); 
// => <child id="1">MyText<child id="2">MyOtherText</child></child> 

爲了確保您得到您的子節點的完整的XML,請嘗試:

var childXML : String = node.children().length() > 0 ? node.children()[0].toXMLString():"empty"); 
trace("node.child#0:"+ childXML); 
// output: 
// node.child#0:<Requirement ID="REQ-BUS-3.2.1" title="exigence bus3" description="blabla 321" testable="true"/> 
+0

好的,謝謝,我的壞^^。 – TheFrenchGuy 2012-02-20 13:39:31