The following example shows how you can ignore comment tags (<!-- … -->) in an XML document by setting the static XML.ignoreComments property.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/22/ignoring-comments-in-an-xml-file-in-actionscript-30/ -->
<mx:Application name="XML_ignoreComments_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            XML.ignoreComments = false;

            private const xmlObj:XML = <TestCase>
                        <setup>
                            <!-- TODO: implement -->
                        </setup>
                        <body>
                            <!-- TODO: implement -->
                        </body>
                    </TestCase>;

            private function init():void {
                XML.ignoreComments = ignoreCommentsCheckBox.selected;

                var newXMLObj:XML = xmlObj.copy();
                textArea.text = newXMLObj.toXMLString();
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="ignoreComments:">
                <mx:CheckBox id="ignoreCommentsCheckBox"
                        change="init();" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:TextArea id="textArea"
            editable="false"
            width="100%"
            height="100%"
            creationComplete="init();" />

</mx:Application>

View source is enabled in the following example.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

One Response to Ignoring comments in an XML file in ActionScript 3.0

  1. Margaret says:

    Thank you Peter, couldn’t figure out why my xml comments were getting dropped until I came across this blog.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree