我如何从Xml数据中获取所有子节点? [重复]

时间:2022-03-22 08:43:58

Possible Duplicate:
how I get child node from each parent node seperately?

可能重复:我如何从每个父节点单独获取子节点?

I have Some XML data..I want to create certain component based on this XML.My XML data is given below

我有一些XML数据..我想基于这个XML创建某个组件。下面给出了我的XML数据

 <main>
     <TabNavigator x="27" y="11" width="455" height="376" id="gh" backgroundColor="#A4B6E9">                                                         <NavigatorContent width="100%" height="100%" label="Client" id="clientTab">
   <Label x="10" y="30" width="52" height="25" text="Name:"/>
  <Label x="10" y="127" width="52" height="28" text="Addres"/>
<TextInput id="name_client" x="69" y="18" width="188" height="37" restrict="[A-Z a-z]"/>
<TextArea id="address_client" x="70" y="70" height="126"/>
<Label x="10" y="230" width="84" height="32" text="Phone:"/>
<TextInput id="phone_client" x="70" y="218" width="188" height="30" restrict="0-9" maxChars="10"/>
<Button x="100" y="291" height="28" label="Submit" click="submitClick()"/>
<Label id="errorClient" x="59" y="270" width="171" height="27" text="please fill the blank fields" color="red" visible="false"/>
</NavigatorContent><NavigatorContent width="100%" height="100%" label="Admin" id="adminTab">
<Label x="23" y="48" width="52" height="25" text="Name:"/>
<Label x="26" y="148" width="52" height="28" text="Addres"/><TextInput id="name_admin" x="105" y="33" width="188" height="37"/>
<TextArea id="address_admin" x="105" y="93" height="126"/>
<Label x="26" y="257" width="84" height="32" text="Phone:"/>
<TextInput id="phone_admin" x="104" y="246" width="188" height="30" restrict="0-9" maxChars="10"/>
<Button x="137" y="305" height="28" label="Submit"/>
<Label id="errorAdmin" x="100" y="286" width="171" height="17" color="red" fontSize="14" text="please fill the blank fields" visible="false"/>
<Button x="335" y="60" height="34" label="Admin Details"/>
<Button x="335" y="180" height="34" label="Client Details"/>
</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 am using the following code for finding the child nodes from the above XML.

我使用以下代码从上面的XML中查找子节点。

   private function loadXML(targetURL:String):void 
                {
                    urlLdr.load(new URLRequest(targetURL));
                    urlLdr.addEventListener(Event.COMPLETE,urlLdr_complete);
                }
                private function urlLdr_complete(event:Event):void
                {

                    var xmlData:XML=new XML(URLLoader(event.currentTarget).data);                       
                        for each (var t:XML in xmlData.children()) 
                        {
                                            Alet.show(t.Name());
                                            }
                            }}

But I only 2 Children nodes(TabNavigator and NavigatorContent).How i get all the children Nodes? Can any one help me please???

但我只有2个子节点(TabNavigator和NavigatorContent)。我如何得到所有孩子的节点?有人可以帮我吗???


My code is given below. I got only child nodes now.. haven't got the parent node.please help me for getting the parent and child nodes..

我的代码如下。我现在只有子节点..没有父节点。请帮助我获取父节点和子节点..

<fx:Script>
        <![CDATA[
            import flashx.textLayout.elements.BreakElement;
            import flashx.textLayout.formats.BackgroundColor;

            import mx.charts.chartClasses.DataDescription;
            import mx.collections.ArrayCollection;
            import mx.collections.XMLListCollection;
            import mx.containers.Canvas;
            import mx.containers.TabNavigator;
            import mx.controls.Alert;
            import mx.controls.Button;
            import mx.controls.Label;
            import mx.controls.Text;




            private var urlLdr:URLLoader=new URLLoader;
            private var childName:String;
            private var i:int=0;
            private var arrayCollection:ArrayCollection=new ArrayCollection;
            private function loadXML(targetURL:String):void 
            {
                urlLdr.load(new URLRequest(targetURL));
                urlLdr.addEventListener(Event.COMPLETE,urlLdr_complete);
            }
            private function urlLdr_complete(event:Event):void
            {

                var xmlData:XML=new XML(URLLoader(event.currentTarget).data);
                handleOneNode(xmlData);
            }
            private function handleOneNode(node:XML,parent:XML=null):void 
            {
            var children:XMLList=node.children();
            if(children.length()==0)
            {i++;
                childName=node.name();
                switch(childName.toString())
                {
                    case "Button":
                    {

                        var myButton:Button=new Button();
                        myButton.x=node..@x;
                        myButton.y=node..@y;
                        myButton.height=node..@height;
                        myButton.width=node..@width;
                        myButton.label=node..@label;
                        myCanvas.addChild(myButton);
                        break;
                    }
                    case "TabNavigator":
                    {
                        var myTabNavigator:TabNavigator=new TabNavigator();
                        myTabNavigator.x=node..@x;
                        myTabNavigator.y=node..@x;
                        myTabNavigator.height=node..@height;
                        myTabNavigator.id=node..@id;
                        myTabNavigator.width=node..@width;
                        break;
                    }
                    case "Datechooser":
                    {
                        break;
                    }
                    case "Label":
                    {
                        var myLabel:Label=new Label();
                        myLabel.x=node..@x;
                        myLabel.y=node..@x;
                        myLabel.height=node..@height;
                        myLabel.id=node..@id;
                        myLabel.width=node..@width;
                        myLabel.text=node..@text;
                        myCanvas.addChild(myLabel);
                    }
                    case "TextInput":
                        var myText:Text=new Text;
                        myLabel.x=node..@x;
                        myLabel.y=node..@x;
                        myLabel.height=node..@height;
                        myLabel.id=node..@id;
                        myLabel.width=node..@width;
                        myLabel.text=node..@text;
                        myCanvas.addChild(myLabel);
                    case "TitleWindow":
                    {
                    }
                    default:
                    {
                    }
                }

            }
            else
            {
                for each(var child:XML in children)
                {
                    handleOneNode(child,node);
                }
            }
            }

        ]]>
    </fx:Script>

1 个解决方案

#1


1  

The children() method of class XML only returns the direct children of the current object. If you want to go through the whole document you need to recursively call your method on each children.

类XML的children()方法只返回当前对象的直接子节点。如果您想浏览整个文档,则需要以递归方式为每个孩子调用您的方法。

So you need to write something like:

所以你需要写一些像:

private function handleOneNode(node:XML):void {
   var children:XMLList = node.children();
   if ( children.length() == 0 ) {
      //Handle Leaf node -> Create ui object, or whatever 
   } else {
      //Non terminal node, check children
      for each (var child:XML in children ) {
         handleOneNode(child);
      }
   }
}

And in your handler you call it:

在你的处理程序中,你称之为:

var xmlData:XML=new XML(URLLoader(event.currentTarget).data);
handleOneNode(xmlData)

EDIT In order to access the parent node you simply have to add an additional parameter to the handleOneNode function.

编辑为了访问父节点,您只需向handleOneNode函数添加一个附加参数。

private function handleOneNode(node:XML, parent:XML=null):void {
       var children:XMLList = node.children();
       if ( children.length() == 0 ) {
          //Handle Leaf node -> Create ui object, or whatever 
       } else {
          //Non terminal node, check children
          for each (var child:XML in children ) {
             handleOneNode(child, node);
          }
       }
    }

And in your handler you call it:

在你的处理程序中,你称之为:

var xmlData:XML=new XML(URLLoader(event.currentTarget).data);
handleOneNode(xmlData)

#1


1  

The children() method of class XML only returns the direct children of the current object. If you want to go through the whole document you need to recursively call your method on each children.

类XML的children()方法只返回当前对象的直接子节点。如果您想浏览整个文档,则需要以递归方式为每个孩子调用您的方法。

So you need to write something like:

所以你需要写一些像:

private function handleOneNode(node:XML):void {
   var children:XMLList = node.children();
   if ( children.length() == 0 ) {
      //Handle Leaf node -> Create ui object, or whatever 
   } else {
      //Non terminal node, check children
      for each (var child:XML in children ) {
         handleOneNode(child);
      }
   }
}

And in your handler you call it:

在你的处理程序中,你称之为:

var xmlData:XML=new XML(URLLoader(event.currentTarget).data);
handleOneNode(xmlData)

EDIT In order to access the parent node you simply have to add an additional parameter to the handleOneNode function.

编辑为了访问父节点,您只需向handleOneNode函数添加一个附加参数。

private function handleOneNode(node:XML, parent:XML=null):void {
       var children:XMLList = node.children();
       if ( children.length() == 0 ) {
          //Handle Leaf node -> Create ui object, or whatever 
       } else {
          //Non terminal node, check children
          for each (var child:XML in children ) {
             handleOneNode(child, node);
          }
       }
    }

And in your handler you call it:

在你的处理程序中,你称之为:

var xmlData:XML=new XML(URLLoader(event.currentTarget).data);
handleOneNode(xmlData)