XMLList

XMLList的特点就是没有根节点。


1直接赋值XMLList的写法


<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600"
creationComplete="application1_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
// 1直接赋值
var xmlList1:XMLList =
<>
<node age="18" label="tomcat" />
<node age="20" label="java" />
<node age="30" label="webLogic" />
</>;
trace(xmlList1);


}

]]>
</fx:Script>
</s:Application>


注意:

  • XMLList虽然是没有根节点,
  • 但是如果在直接赋值的时候,还是需要一对空标签( <> </> ),将内容包起来!!!!
  • 赋值的时候,一对空标签是不会赋值给XMLList的。
  • 所以trace的时候,一对空标签一定不会打印出来的。


2通过XML取得XMLList的方法如下:



<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600"
creationComplete="application1_creationCompleteHandler(event)" xmlns:vo="vo.*">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
var xml:XML =
<root>
<node age="18" label="tomcat" >
<item version="5.0" />
<item version="6.0" />
<item version="7.0" />
</node>
<node age="20" label="java" >
<item version="1.5" >
<node version="xx" />
<node version="yy" />
<node version="zz" />
</item>
<item version="1.6" />
<item version="1.7" />
</node>
<node age="30" label="webLogic" >
</node>
</root>;

var xmlList2:XMLList = xml.elements("node");
trace("---------------------");
trace(xmlList2);

}

]]>
</fx:Script>

</s:Application>


注意,经过测试,XML.elements()方法可以返回仅仅是下一级别的XMLList



console:



<node age="18" label="tomcat">
  <item version="5.0"/>
  <item version="6.0"/>
  <item version="7.0"/>
</node>
<node age="20" label="java">
  <item version="1.5">
    <node version="xx"/>
    <node version="yy"/>
    <node version="zz"/>
  </item>
  <item version="1.6"/>
  <item version="1.7"/>
</node>
<node age="30" label="webLogic"/>




3 通过XMLList取得XMLList的方法如下:



<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600"
creationComplete="application1_creationCompleteHandler(event)" xmlns:vo="vo.*">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
var xml:XML =
<root>
<node age="18" label="tomcat" >
<item version="5.0" />
<item version="6.0" />
<item version="7.0" />
</node>
<node age="20" label="java" >
<item version="1.5" >
<node version="xx" />
<node version="yy" />
<node version="zz" />
</item>
<item version="1.6" />
<item version="1.7" />
</node>
<node age="30" label="webLogic" >
</node>
</root>;

var xmlList2:XMLList = xml.elements("node");
trace("---------------------");
trace(xmlList2);

var xmlList3:XMLList = xmlList2.elements("item");
trace("---------------------");
trace(xmlList3);
}

]]>
</fx:Script>



</s:Application>


console:



---------------------
<node age="18" label="tomcat">
  <item version="5.0"/>
  <item version="6.0"/>
  <item version="7.0"/>
</node>
<node age="20" label="java">
  <item version="1.5">
    <node version="xx"/>
    <node version="yy"/>
    <node version="zz"/>
  </item>
  <item version="1.6"/>
  <item version="1.7"/>
</node>
<node age="30" label="webLogic"/>
---------------------
<item version="5.0"/>
<item version="6.0"/>
<item version="7.0"/>
<item version="1.5">
  <node version="xx"/>
  <node version="yy"/>
  <node version="zz"/>
</item>
<item version="1.6"/>
<item version="1.7"/>




注意,XMLList.elements()方法可以返回当前XMLList所有一级节点的下一级别的XMLList





4 标签定义写法:



<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600"
creationComplete="application1_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
trace("---------------------");
trace(xml1);
}

]]>
</fx:Script>

<fx:Declarations>
<fx:XMLList id="xml1">
<node age="18" label="tomcat" >
<item version="5.0" />
<item version="6.0" />
<item version="7.0" />
</node>
<node age="20" label="java" >
<item version="1.5" >
<node version="xx" />
<node version="yy" />
<node version="zz" />
</item>
<item version="1.6" />
<item version="1.7" />
</node>
<node age="30" label="webLogic" >
</node>
</fx:XMLList>
</fx:Declarations>

</s:Application>


注意:此时,就不可以使用,一对空标签( <> </> )了!!!!