1、XML和XMLList有什么区别呢,很简单,看是否含有根节点。举例来说:
<root>
<node type="student" name="学生一">
<node type="student" name="学生二">
<node type="teacher" name="老师一">
</root>
这就是一个典型的XML,其中<root>是根节点,<node>是子节点。type,name是属性名,等号右边是属性值。而
<node type="student" name="学生一" />
<node type="student" name="学生二" />
<node type="teacher" name="老师一" />
这一部分就是一个XMLList。
注:<node type="student" name="学生一" />也可以转成成XML对象。这点在遍历xmllist时会使用到。
2、遍历xmllist。
下面是一段代码,简单介绍了从xml-->xmllist,然后遍历xmllist的过程
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal" verticalAlign="middle" horizontalAlign="center" backgroundGradientColors="[0x000000,0x323232]" viewSourceURL="srcview/index.html" creationComplete="init();"> <mx:Script> <![CDATA[ import mx.controls.Alert; public function init():void{ var books:XML; books = <books> <book publisher="Addison-Wesley" name="book1" /> <book publisher="Addison-Wesley" name="book2" /> <book publisher="Addison-Wesley" name="book3" /> <book publisher="Addison-Wesley" name="book4" /> <book publisher="O'Reilly Media" name="book5" /> <book publisher="O'Reilly Media" name="book6" /> </books>; //将xml转成xmllist(取子项,下面两个方法等价) // var bookList:XMLList = books.child("*"); var bookList:XMLList = books.children(); xmllist.text = bookList; //遍历xmllist var i:int = 0; for each( var book:XML in bookList){ i++; switch(i){ case 1: //xml的取值方式如下 x1.text = book.@name; break; case 2: x2.text = book.@name; break; case 3: x3.text = book.@name; break; case 4: x4.text = book.@name; break; case 5: x5.text = book.@name; break; case 6: x6.text = book.@name; break; default: break; } } } ]]> </mx:Script> <mx:VBox> <mx:TextArea id="xmllist" width="500" height="100"> </mx:TextArea> <mx:TextArea id="x1" width="500" height="50"> </mx:TextArea> <mx:TextArea id="x2" width="500" height="50"> </mx:TextArea> <mx:TextArea id="x3" width="500" height="50"> </mx:TextArea> <mx:TextArea id="x4" width="500" height="50"> </mx:TextArea> <mx:TextArea id="x5" width="500" height="50"> </mx:TextArea> <mx:TextArea id="x6" width="500" height="50"> </mx:TextArea> </mx:VBox> </mx:Application>
执行结果: