我有一些数据生成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>
我用下面的code,用于检索给定的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 seperate children for each parent. How can I get it? Could anyone help me please..?
您需要使用递归函数来走的树。使用跟踪,而不是警报()():
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.