flex xml Children() 方法奇怪的行为

发布于 2025-01-06 20:37:53 字数 4147 浏览 3 评论 0原文

我有以下 xml 声明:

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 填充了高级数据网格,但问题是未检测到要求:

我已经重写了 HierarchicalData 类的方法来查看发生了什么:

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 的源代码,因为它不是开源的。有人能告诉我发生了什么事吗?这是一个错误还是其他什么?

I have the following xml declaration:

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

I populated an advanced datagrid with this xml but the problem is that a requirement isn't detected: <Requirement ID="REQ-BUS-3.2.1" title="exigence bus3" description="blabla 321" testable="true"/>

I've overriden a method of the HierarchicalData class to see what 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());
                }


            } 

And here is the result (console), that I just don't understand at all:

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():

The node isn't detected by the children() method but it exists. The problem is that I can't look at the source code of XML.abc cause it isn't open-source. Can someone tell me what is happenning? Is it a bug or something else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看海 2025-01-13 20:37:53

该节点由 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"/>

The node is picked up by the children() method just the way you intended, because otherwise the length() of children() would be 0. What you have to understand is that trace( node.children()); actually means trace ( XMLList( node.children() ).toString()); - you are calling toString() on an XMLList - the output of which can have a number of different results, though obviously not very predictable ones:

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>

To make sure you get the full xml of your child node, try:

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"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文