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

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

Accessing Comments and Processing Instructions

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



Converting Comments and Processing-Instructions to Strings

 

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">
        <!--This is a comment-->
            <?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(  ));
        
    }
  }
}



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

 
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!
                                     <!-- Start PHP -->
                                        <?php
                                           // execute some php code
                                        ?>
                                     <!-- End PHP -->
                                  </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:
    }
  }
}



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

 

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"));
    }
  }
}



Setting Options for the XML Class

 
//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
    }
  }
}



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

 
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
    }
  }
}



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

 

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!
                                     <!-- Start PHP -->
                                        <?php
                                           // execute some php code
                                        ?>
                                     <!-- End PHP -->
                                  </body>
                               </html>;
        trace(example.*.ruments().length()); // Displays: 2
        trace(example.body.ruments().toXMLString());
        trace(example.body.ruments()[1].toXMLString());
        trace(example.*.processingInstructions().toXMLString());

    }
  }
}