·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3的解析xml详细源代码

时间:2014-05-15 09:17酷播
先建立一个类文件,然后拷贝下面代码张贴进类文件中,注意类文件的名字,再建立一个fla文件,捆绑刚才的类

先建立一个类文件,然后拷贝下面代码张贴进类文件中,注意类文件的名字.
再建立一个fla文件,捆绑刚才的类.然后编译下看看 ^_^

  1. package { 
  2.     import flash.display.Sprite; 
  3.  
  4.     public class XmlExample extends Sprite {        
  5.         public function XmlExample() { 
  6.             var employees:XML = 
  7.                <employees> 
  8.                     <employee ssn="123-123-1234"> 
  9.                         <name first="John" last="Doe"/> 
  10.                         <address> 
  11.                             <street>11 Main St.</street> 
  12.                             <city>San Francisco</city> 
  13.                             <state>CA</state> 
  14.                             <zip>98765</zip> 
  15.                         </address> 
  16.                     </employee> 
  17.                     <employee ssn="789-789-7890"> 
  18.                         <name first="Mary" last="Roe"/> 
  19.                         <address> 
  20.                             <street>99 Broad St.</street> 
  21.                             <city>Newton</city> 
  22.                             <state>MA</state> 
  23.                             <zip>01234</zip> 
  24.                         </address> 
  25.                     </employee> 
  26.                 </employees>
  27.  
  28.             trace(employees.employee[0].address.zip);   // 98765 
  29.  
  30.             trace(employees.employee[1].@ssn);          // 789-789-7890 
  31.  
  32.             trace(employees.employee.name);             // <name first="John" last="Doe"/> 
  33.                                                         // <name first="Mary" last="Roe"/> 
  34.  
  35.             trace(employees..zip[0]);                   // 98765 
  36.  
  37.             trace(employees..@ssn[1]);                  // 789-789-7890 
  38.  
  39.             trace(employees..name);                     // <name first="John" last="Doe"/> 
  40.                                                         // <name first="Mary" last="Roe"/> 
  41.  
  42.             trace(employees.employee[0].address.*);     // <street>11 Main St.</street>   
  43.                                                         // <city>San Francisco</city> 
  44.                                                         // <state>CA</state> 
  45.                                                         // <zip>98765</zip> 
  46.             var node:String = "zip"
  47.             trace(employees.employee[0].address[node]); // 98765 
  48.             
  49.             var attribute:String = "ssn"
  50.             trace(employees.employee[1].@[attribute]); // 789-789-7890 
  51.             
  52.             for each (var num:XML in employees..@ssn) { 
  53.                 trace(num);                             // 123-123-1234 
  54.             }                                           // 789-789-7890 
  55.             
  56.             var ssnToFind:String = "789-789-7890"
  57.             trace(employees.employee.(@ssn == ssnToFind).toXMLString()); 
  58.                                                         // <employee ssn="789-789-7890"> 
  59.                                                         // <name first="Mary" last="Roe"/> 
  60.                                                         // <address> 
  61.                                                         // <street>99 Broad St.</street> 
  62.                                                         // <city>Newton</city> 
  63.                                                         // <state>MA</state> 
  64.                                                         // <zip>01234</zip> 
  65.                                                         // </address> 
  66.                                                         // </employee> 
  67.     
  68.  
  69. trace(employees.employee.( zip==98765).toXMLString()); 
  70.  
  71. //<employee ssn="123-123-1234"> 
  72.                         <name first="John" last="Doe"/> 
  73.                         <address> 
  74.                             <street>11 Main St.</street> 
  75.                             <city>San Francisco</city> 
  76.                             <state>CA</state> 
  77.                             <zip>98765</zip> 
  78.                         </address> 
  79.                     </employee> 
  80.  
  81.     } 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器