I have some Data Xml..
我有一些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>
I use following code for retrieving the child names of given XML..
我使用以下代码来检索给定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);
}
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?
但我只得到2个孩子(TabNavigator和TitleWindow)。我如何在每个父节点中获得其他孩子?我希望每个父母都有独立的孩子。我怎么才能得到它?
1 个解决方案
#1
1
You need to use a recursive function to walk down the tree. Using trace() instead of alert():
您需要使用递归函数向下走树。使用trace()而不是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);
}
}
}
Or, use the E4X descendants() function:
或者,使用E4X descendants()函数:
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.
我没有测试,但我希望内置的descendants()函数更有效。
#1
1
You need to use a recursive function to walk down the tree. Using trace() instead of alert():
您需要使用递归函数向下走树。使用trace()而不是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);
}
}
}
Or, use the E4X descendants() function:
或者,使用E4X descendants()函数:
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.
我没有测试,但我希望内置的descendants()函数更有效。