trace str和trace xml的不同

简介: var str:String = "";         var strXML:XML = new XML(str);      trace(str);   trace(strXML); 请大家想一下,输出结果是什么?--------------------------------    ...

var str:String = "<?xml version=\"1.0\" encode=\"UTF-8\"?><a><b><c res=\"true\" /></b></a>";
   
   
   var strXML:XML = new XML(str);
   
   trace(str);
   trace(strXML);

请大家想一下,输出结果是什么?--------------------------------

 

<?xml version="1.0" encode="UTF-8"?><a><b><c res="true" /></b></a>


<a>
  <b>
    <c res="true"/>
  </b>
</a>

 

可以看出输出xml,xml的头部没有了,每个结点前面加一个空格,而且strXML相当于a结点

如果我们要输出c结点的属性res

 trace("@:" + strXML.b.c[0].@res);

 

 

XML处理技巧一则

 

 

 由于AS3中XML 支持E4X 规范的 XML 处理..
所以我们可以方便的使用
xml.(条件)来过滤我们需要的节点..
如:

01.var xl:XML = <root>
02.            <item type="1">http://adobe.com </item>
03.            <item type="1">http://l4cd.net </item>
04.            <item type="2">http://google.com </item>
05.            <item type="2">http://qq.com </item>
06.        </root>
07.trace(xl.item.(@type == 1));
08.//output:
09.//<item type="1">http://adobe.com </item>
10.//<item type="1">http://l4cd.net </item>


但是有时候会遇到这样一种情况..并不是每个节点都包含指定的属性..
如:

1.<root>
2.    <item type="1">http://adobe.com </item>
3.    <item type="1">http://l4cd.net </item>
4.    <item type="2">http://google.com </item>
5.    <item>http://qq.com </item>
6.</root>

这时候如果还继续使用xl.item.(@type == 1),将出现以下错误

1.ReferenceError: Error #1065: 变量 @type 未定义。
2.    at Main()[X:\Main.as:21]


为避免这种情况..可以使用hasOwnProperty方法..先判断是否有指定的属性~

01.var xl:XML = <root>
02.            <item type="1">http://adobe.com </item>
03.            <item type="1">http://l4cd.net </item>
04.            <item type="2">http://google.com </item>
05.            <item>http://qq.com </item>
06.        </root>
07.trace(xl.item.(hasOwnProperty('@type') && @type == 1));
08.//output:
09.//<item type="1">http://adobe.com </item>
10.//<item type="1">http://l4cd.net </item>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

img_a6339ee3e57d1d52bc7d02b338e15a60.gif   XML.ignoreComments  =  false;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  var xml:XML 
=  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< body >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< ! --  comment  -->
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  text1
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< a >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< b > text2 </ b >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
</ a >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
</ body > ;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace(xml.descendants(
" * " ).length());  //   5
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace(xml.descendants(
" * " )[0]);  //   //   < ! --  comment  -->
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace(xml.descendants(
" * " )[ 1 ].toXMLString());  //  text1
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace(xml.descendants(
" a " ).toXMLString());  //   < a >< b > text2 </ b ></ a >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace(xml.descendants(
" b " ).toXMLString());  //   < b > text2 </ b >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif

为什么放弃AS2.0选择AS3.0?如果只允许我说三个理由。那么AS3.0对XML的近乎完美的支持绝对是其中一个。
简单说说AS3.0中对于XML支持的不同吧:
A.AS2.0对XML的支持勉勉强强,将就着可以用。而AS3.0中对XML的支持是全方位的,极其强大和灵活的。
B.AS2.0对XML的支持不是内建的(build-in),也并非基于ECMAScript for XML(E4X)标准。而AS3.0中对XML的支持符合E4X标准,它的设计有三个优点:
1. 简易。包括操作和可读性。你会发现AS3.0中对于XML的操作犹如对一个普通Object对象一样浅显易懂。语句非常浅白流畅。
2. 连续性。其各个功能的设计和AS3.0其余的部分思想一致,易于理解。
3. 熟悉。操作符和操作逻辑对我们来说都相当熟悉易用。
在AS2.0时代,为了解决这部分的问题

C.效率。
效率包括两方面,开发效率,和代码执行效率。开发效率的论述见上。AS3.0对于XML的执行效率远远高过没有内建XML支持的AS2.0。

<strong>XML的输入</strong>
在AS2.0时代,在代码行中输入XML代码是一种痛苦。如果不是从文件中读取,那么我们就要忍受一长串挤在一块儿的字符串。

而在AS3.0中,太简单了。直接按照XML的内容输即可,想换行就换行,想Tab就Tab,就一个字,爽。
新建一个fla,选中第一帧,F9打开动作面板,输入如下代码:

img_a6339ee3e57d1d52bc7d02b338e15a60.gif   // 【黑羽】ActionScript  3 .0系列教程( 4 )
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
// http: // www.kingda.org
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
// 例1
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  var kingdaXML:XML 
=  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< tutorial >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< item id = ' 1 ' >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< level > 2 </ level >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< title >     First touch of Flash  9 </ title >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
</ item >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< item id = ' 2 ' >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< level > 3 </ level >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< title >     Binding Classes </ title >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
</ item >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< item id = ' 3 ' >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< level > 4 </ level >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< title > Document Class </ title >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
</ item >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
</ tutorial >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace (kingdaXML.item[
1 ].level);   // output: 3
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
// 例2
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  var kS:String 
=   " <root><txt>this is a test</txt></root> " ;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  var kXML:XML 
=  new XML(kS);
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace (kXML.txt); 
// output:this  is   a test;

例1中注意到没,直接写XML内容在后面,想换行就换行,想tab就tab,多爽。不想AS2.0中写string时,换个行就不行了。
写完这一句后,我们所写出的类似于string的形式立刻就被Flash理解成了XML对象了,所以我们马上就可以用"."操作符来访问相应的属性。本例中访问了第2个item节点的level值。
这么简便直观的访问方式是不是比 AS2.0中那千遍一律的childNodes要好得多?<img src="/mt-static/smilies/laughing.gif" width="20" height="20" border="0" alt="laughing.gif" title="funny!" />

 

不过要注意,最后可以加";"结束。但我为了XML的视觉美观没有加。这个没有关系,编译时不会考虑这一点。
<strong>事实上只要你喜欢,AS1.0, 2.0, 3.0中语句结束都可以不加";"号。但是这并不是一个好的编程习惯,更不符合严谨的自我语法要求。因此我建议,除了XML可以不加外,其余的都应该加,呵呵。</strong>

例2展示了如何将一个包含了XML内容的字符串转换成XML对象。用的是XML的构造函数转换的。

AS3更有趣的是,可以使用已有的变量来直接构造XML,带来方便的编程特性。如下例。


img_a6339ee3e57d1d52bc7d02b338e15a60.gif var rootNodeName    :String  =   " site "
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  var subNodeName        :String 
=   " orgin " ;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  var subNodeContent    :String 
=   " Kingda's Blog "
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  var attributeName    :String 
=   " url "
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  var attributeValue    :String 
=   " http://www.kingda.org "
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  var extXML:XML 
=  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< {rootNodeName} {attributeName} = {attributeValue} >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< {subNodeName} > {subNodeContent} </ {subNodeName} >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
</ {rootNodeName} > ;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace (extXML.toString());
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
/* output:
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< site url = " http://www.kingda.org " >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< orgin > Kingda ' s Blog</orgin>
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
   </ site >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
*/
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
< strong > 要点就是要把变量用 " {} " 括起来,并且设置属性时不要再加引号了。 </ strong >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif这个特性黑羽非常喜欢。
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
< strong > XML的外部读取 </ strong >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif包括读取外部xml文件,和通过URL读取xml。AS3.0中不像2.0那样集成了一个load()。
img_a6339ee3e57d1d52bc7d02b338e15a60.gifAS3.0在架构上就设计了所有与外部打交道的都由URLrequest对象来进行,数据都由URLloader对象来接受。这个我们会在下一部分教程详细讲解。这一次只要知道这样的架构设计是深思熟虑,且简洁优美的即可。
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  var myXML:XML 
=  new XML();
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
// 初始化XML地址,可以是本地的 " xxx.xml " ,也可以是如下的URL地址。
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  var XML_URL:String 
=   " http://www.kingda.org/blog/index.xml " ;        // 我的Blog RSS Feed
img_a6339ee3e57d1d52bc7d02b338e15a60.gifvar myXMLURL:URLRequest 
=  new URLRequest(XML_URL);
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  var myLoader:URLLoader 
=  new URLLoader(myXMLURL);
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
// 添加装载完成侦听器,
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
// Event.COMPLETE的值是 " complete " ,直接用此字符串也可以。
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
img_a6339ee3e57d1d52bc7d02b338e15a60.giffunction xmlLoaded(evtObj:Event) { 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  myXML 
=  XML(myLoader.data);
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace(
" 数据装载完成. " );
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace (myXML);
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  }
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
< strong > XML的操作。 </ strong >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif

精彩的部分到了。详细看我下面的例子代码。

1.查询

img_a6339ee3e57d1d52bc7d02b338e15a60.gif // 显示level为4的节点的title值
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace (kingdaXML.item.(level 
==   4 ).title);
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
// output:Document Class
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
// 显示level > 2的节点的title值,本处结果大于1,所以是一个XML Array。
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace (kingdaXML.item.(level 
>   2 ).title);
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
/* output:
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< title > Binding Classes </ title >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< title > Document Class </ title >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
*/
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
// 使用属性用@开头即可。真方便。
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace (kingdaXML.item.(level 
>   2 ).@id); 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
// output: 23
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
// 这儿要注意,实际上是2, 3 。一个Array.
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
// 也可以用属性来做判断
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace (kingdaXML.item.(@id 
>   1 ).title);
img_a6339ee3e57d1d52bc7d02b338e15a60.gif 2 .添加或者修改属性
img_a6339ee3e57d1d52bc7d02b338e15a60.gif方便的不能再方便,直接写即可。爽翻天啊。
img_a6339ee3e57d1d52bc7d02b338e15a60.gif   // 把id  ==  1的节点level值改为2
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  kingdaXML.item.(@id
== 1 ).level  =   2 ;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
// 把id == 1的节点添加一个属性 page
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  kingdaXML.item.(@id
== 1 ).page  =   100 ;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace (kingdaXML.item.(@id
== 1 ));
img_a6339ee3e57d1d52bc7d02b338e15a60.gif


 3.按某条件插入节点

img_a6339ee3e57d1d52bc7d02b338e15a60.gif var newNode1:XML  =   < item id = ' 2.5 ' >< level > 0 </ level >< title > None </ title ></ item >  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  var newNode2:XML 
=   < item id = ' 1.5 ' >< level > 0 </ level >< title > None </ title ></ item >  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
// 把newNode1插入到id == 2的节点后面
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  kingdaXML 
=  kingdaXML.insertChildAfter(kingdaXML.item.(@id == 2 ), newNode1);
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
// 把newNode1插入到id == 2的节点前面
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  kingdaXML 
=  kingdaXML.insertChildBefore(kingdaXML.item.(@id == 2 ), newNode2);
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace (kingdaXML);
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
< strong > XML的高级操作。 </ strong >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif


常用的操作上面已经介绍的很清楚了。高级操作则是留给对XML应用更深的兄弟们。
几点注意:
1.在AS3.0中, XML类的ignoreWhitespace默认为true。
2.AS3.0支持对comments的直接操作。但默认

 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif XML.ignoreComments  =  false;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  var kingdaXML:XML 
=  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< item >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< ! --  comment   1 -->
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< ! --  comment   2 -->
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
</ item > ;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace(kingdaXML.toXMLString());  
// 默认为true时,不会显示comment的
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif访问comment用
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace(kingdaXML.comments()[
1 ].toXMLString()); 

3.XML支持克隆。
使用copy()可以得到一份现有XML的值拷贝。


var kingdaCopy:XML = kingdaXML.copy();


对kingdaCopy操作就不会影响kingdaXML对象了。
4.极有用的descendants函数返回一个XMLList对象,包括所有的子节点。
设ignoreComments = false;和ignoreProcessingInstructions = false后,连comments和process instructions也会包含在这个XMLList对象中。
运用示例如下:

img_a6339ee3e57d1d52bc7d02b338e15a60.gif   XML.ignoreComments  =  false;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  var xml:XML 
=  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< body >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< ! --  comment  -->
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  text1
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< a >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
< b > text2 </ b >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
</ a >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
</ body > ;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace(xml.descendants(
" * " ).length());  //   5
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace(xml.descendants(
" * " )[0]);  //   //   < ! --  comment  -->
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace(xml.descendants(
" * " )[ 1 ].toXMLString());  //  text1
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace(xml.descendants(
" a " ).toXMLString());  //   < a >< b > text2 </ b ></ a >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  trace(xml.descendants(
" b " ).toXMLString());  //   < b > text2 </ b >
img_a6339ee3e57d1d52bc7d02b338e15a60.gif

还有太多的XML有用操作功能了(如对namespace的操作)。用到时再去翻参考书吧。
以上的介绍可以满足绝大部分运用了。

 

打完收工,歇歇。
对了AS2.0已有的XML类,在3.0中变成了XMLDocument类,使用方法不变。便于AS2.0程序移植。其余不推荐。

 

目录
打赏
0
0
0
0
13
分享
相关文章
|
10月前
Could not find method debug()
Could not find method debug()
274 59
|
10月前
|
SpringBoot集成log4j2出现Logback configuration error detected: current ElementPath is
SpringBoot集成log4j2出现Logback configuration error detected: current ElementPath is
143 0
|
10月前
log4j2.xml的日志打印配置
log4j2.xml的日志打印配置
113 0
To see the full stack trace of the errors, re-run Maven with the -e switch.
问题描述 微服务项目中其它模块不能依赖common模块中的依赖 而common中是有依赖的
1004 1
To see the full stack trace of the errors, re-run Maven with the -e switch.
AttributeError: ‘version_info‘ object has no attribute ‘version‘
AttributeError: ‘version_info‘ object has no attribute ‘version‘
267 0
对‘avformat_find_stream_info’未定义的引用、to the PKG_CONFIG_PATH environment variable
对‘avformat_find_stream_info’未定义的引用、to the PKG_CONFIG_PATH environment variable
104 0
因为CircleImageView导致Binary XML file line #96: Error inflating class <unknown>
因为CircleImageView导致Binary XML file line #96: Error inflating class <unknown>
183 0
使用vs2015打开.pro文件报错:Project ERROR: Cannot run compiler 'cl' -- 完美解决
使用vs2015打开.pro文件报错:Project ERROR: Cannot run compiler 'cl' -- 完美解决
1482 0
使用vs2015打开.pro文件报错:Project ERROR: Cannot run compiler 'cl' -- 完美解决
1--debug时安卓源码不一致问题--Source code does not match the bytecode
AS--debug时:Source code does not match the bytecode 解决方案:保持模拟器与编译版本一致,即: 编译版本与运行版本一致
3666 0