Ignoring comments in an XML file in ActionScript 3.0

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.

One thought on “Ignoring comments in an XML file in ActionScript 3.0

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

Comments are closed.