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

Get haXe 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.