Flex之旅:第一部分:flex必备基础知识积累(6)---XML, XMLList, XMLListCollection的区别(二)

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>

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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值