问题描述
我有一些数据 Xml..
I have some Data Xml..
<main>
<TabNavigator x="27" y="11" width="455" height="376" id="gh" backgroundColor="#A4B6E9">
<NavigatorContent width="100%" height="100%" label="Client" id="clientTab"></NavigatorContent>
<NavigatorContent width="100%" height="100%" label="Admin" id="adminTab"></NavigatorContent></TabNavigator>
<TitleWindow x="521" y="84" width="377" height="234">
<DataGrid x="0" y="0" width="375" height="163" borderVisible="true" id="details">
<columns>
<ArrayList>
<GridColumn dataField="Name" id="arrayName"/><GridColumn dataField="Address" headerText="Address"/>
<GridColumn dataField="Phone_Number" headerText="Phone_Number"/>
</ArrayList>
</columns>
</DataGrid>
<Button x="139" y="167" height="28" label="Export"/>
</TitleWindow>
</main>
我使用以下代码来检索给定 XML 的子名称..
I use following code for retrieving the child names of given XML..
private function urlLdr_complete(event:Event):void{
var xmlData:XML=new XML(URLLoader(event.currentTarget).data);
for each (var t:XML in xmlData.children())
{
Alert.show(t.Name);
}
但我只有 2 个孩子(TabNavigator 和 TitleWindow).我如何在每个父节点中获得其他孩子?我希望每个父母都有单独的孩子.我怎样才能得到它?
But I only get 2 children(TabNavigator and TitleWindow).How do I get the other children in each parent node? I want separate children for each parent. How can I get it?
推荐答案
您需要使用递归函数沿着树向下走.使用 trace() 而不是 alert():
You need to use a recursive function to walk down the tree. Using trace() instead of alert():
private function urlLdr_complete(event:Event):void
{
var xmlData:XML=new XML(URLLoader(event.currentTarget).data);
showNodeName(xmlData);
}
private function showNodeName($node:XML):void
{
// Trace the current node
trace($node.name());
if($node.hasChildNodes)
{
for each (var child:XML in $node.children())
{
// Recursively call this function on each child
showNodeName(child);
}
}
}
或者,使用 E4X 后代()函数:
Or, use the E4X descendants() function:
private function urlLdr_complete(event:Event):void
{
var xmlData:XML=new XML(URLLoader(event.currentTarget).data);
// Trace the root node:
trace(xmlData.name());
// And trace all its descendants:
for each(var child:XML in xmlData.descendants())
{
trace(child.name());
}
}
两者都应该产生相同的结果:
Both should produce identical outcomes:
main
TabNavigator
NavigatorContent
NavigatorContent
TitleWindow
DataGrid
columns
ArrayList
GridColumn
GridColumn
GridColumn
Button
我还没有测试过,但我希望内置的后代 () 函数更高效.
I haven't tested but I would expect the built-in descendants() function to be more efficient.
这篇关于我如何分别从每个父节点获取子节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!