Flash / Flex / ActionScript/XML/Comments and Processing Instructions

Материал из Web эксперт
Перейти к: навигация, поиск

Accessing Comments and Processing Instructions

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var novel:XML = <BOOK ISBN="0000000000">
           
           <?application someData?>
           <TITLE>ActionScript</TITLE>
           <AUTHOR>J, J</AUTHOR>
           <?app2 someData?>
           <PUBLISHER>Books Ltd</PUBLISHER>
           
         </BOOK>
       
       novel.ignoreComments = false;
       novel.ignoreProcessingInstructions = false;
       
       trace(novel.ruments(  )[0]);                // 
       trace(novel.ruments(  )[1]);                // 
       trace(novel.processingInstructions(  )[0]);  // <?app1 someData?>
       trace(novel.processingInstructions(  )[1]);  // <?app2 someData?>
   }
 }

}

       </source>
   
  


Converting Comments and Processing-Instructions to Strings

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       XML.ignoreComments = false;
       XML.ignoreProcessingInstructions = false;
       
       var novel:XML = <BOOK ISBN="0141182806">
       
           <?someTargetApp someData?>
           <TITLE>Actionscript</TITLE>
           <AUTHOR>J, K</AUTHOR>
           <PUBLISHER>Penguin Books Ltd</PUBLISHER>
         </BOOK>;
       
       trace(novel.ruments()[0].toString(  ));
       
       trace(novel.processingInstructions()[0].toString(  ));
       
   }
 }

}

       </source>
   
  


Finding Node Types: element, attribute, text, comment, processing-instruction

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       XML.ignoreComments = false;
       XML.ignoreProcessingInstructions = false;
       var example:XML = <html>
                                 <head />
                                 <body id="main">
                                    Welcome!
                                    
                                       <?php
                                          // execute some php code
                                       ?>
                                    
                                 </body>
                              </html>;
       trace(example.body.nodeKind()); // Displays: element
       trace(example.body.@id.nodeKind()); // Displays: attribute
       trace(example.body.text().nodeKind()); // Displays: text
       trace(example.body.ruments()[0].nodeKind()); // Displays: comment
       trace(example.body.processingInstructions().nodeKind()); // Displays:
   }
 }

}

       </source>
   
  


Search for elements only while ignoring text nodes, comments, and processing directives, use elements()

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var movieList:XML = <movieList>
                               <listName>My favorite movies</listName>
                               <movie id="123">
                                   <title>Titus</title>
                                   <year>1999</year>
                                   <director>J T</director>
                               </movie>
                               <movie id="456">
                                   <title>Rushmore</title>
                                   <year>1998</year>
                                   <director>W A</director>
                               </movie>
                               <movie id="789">
                                   <title>Hall</title>
                                   <year>1977</year>
                                   <director>Woody Allen</director>
                               </movie>
                          </movieList>;
       
       trace(movieList.elements("year"));
   }
 }

}

       </source>
   
  


Setting Options for the XML Class

   <source lang="java">

//ignoreWhitespace //ignoreComments //ignoreProcessingInstructions //prettyPrinting //prettyIndent //ignoreWhitespace

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       trace(XML.settings().ignoreProcessingInstructions); // Displays: true
       XML.ignoreProcessingInstructions = false;
       trace(XML.settings().ignoreProcessingInstructions); // Displays: false
   }
 }

}

       </source>
   
  


To obtain an XMLList representing all comments and processing instructions within an entire XML tree

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       var novel:XML = <BOOK ISBN="0000000000" INSTOCK="false">
           <TITLE>ActionScript</TITLE>
           <AUTHOR>J, J</AUTHOR>
           <PUBLISHER>Books Ltd</PUBLISHER>
         </BOOK>;
       
       var tempRoot:XML = <tempRoot/>;
       tempRoot.appendChild(novel);
       
       trace(tempRoot..*.ruments(  )[0]);  // First comment in the document
   }
 }

}

       </source>
   
  


use the comments() method or the processingInstructions() method to get XMLLists

   <source lang="java">

package{

 import flash.display.Sprite;
 
 public class Main extends Sprite{
   public function Main(){
       XML.ignoreComments = false;
       XML.ignoreProcessingInstructions = false;
       var example:XML = <html>
                                 <head />
                                 <body id="main">
                                    Welcome!
                                    
                                       <?php
                                          // execute some php code
                                       ?>
                                    
                                 </body>
                              </html>;
       trace(example.*.ruments().length()); // Displays: 2
       trace(example.body.ruments().toXMLString());
       trace(example.body.ruments()[1].toXMLString());
       trace(example.*.processingInstructions().toXMLString());
   }
 }

}

       </source>