Book Image

haXe 2 Beginner's Guide

5 (1)
Book Image

haXe 2 Beginner's Guide

5 (1)

Overview of this book

Table of Contents (21 chapters)
haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – Writing to an XML file


  1. We want to be able to write an XML file. To write the XML file, we will follow the same idea. Add the following function in the Layer class:

    public function toXMLNode() : Xml
    {
       var nXml = Xml.createElement("layer");
       nXml.set("id", this.id);
       return nXml;
    }
  2. Now, add the following code in the Page class:

    public function toXMLFile(path : String)
    {
       var xmlDoc = Xml.createDocument();
       var pageNode = Xml.createElement("page");
       pageNode.set("name", this.name);
       xmlDoc.addChild(pageNode);
       
       for(l in layers)
       {
          pageNode.addChild(l.toXMLNode());
       }
       
       neko.io.File.write(path, false).writeString(xmlDoc.toString());
    }

You can now save a page and all its layers by calling toXMLFile.

What just happened?

We have created a simple of the function in order to be able to save our page as an XML file by calling toXMLFile.