Time for action – Writing to an XML file
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; }
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
.