We are going to create a function to read from an XML file. So, let's proceed step by step.
Layer
class as follows:class Layer { public var id : String; public function new() {} }
Page
class as follows:class Page { public var name : String; public var layers : List<Layer>; public function new() { } }
Page
class:public static function fromXMLFile(path : String) : Page { var nPage = new Page(); var xmlDoc = Xml.parse(neko.io.File.read(path, false). readAll().toString()); nPage.name = xmlDoc.firstElement().get("name"); return nPage; }
Layer
class:public static function fromXMLNode(node : Xml) { var nLayer : Layer; nLayer = new Layer(); nLayer.id = node.get("id"); return nLayer; }
fromXMLFile
function, let's add some code to iterate over the nodes named layers
, and parse them using the fromXMLNode
function:public static function fromXMLFile(path : String) : Page { var nPage = new Page(); var xmlDoc = Xml.parse(neko.io.File.read (path, false).readAll().toString()); nPage.name = xmlDoc.firstElement().get("name"); for(l in xmlDoc.firstElement().elementsNamed("layer")) { nPage.layers.add(Layer.fromXMLNode(l)); } return nPage; }
3.144.9.147