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 – Reading from the XML file


We are going to create a function to read from an XML file. So, let's proceed step by step.

  1. First, create the Layer class as follows:

    class Layer
    {
       public var id : String;
       
       public function new()
       {}
    }
  2. Now create the Page class as follows:

    class Page
    {
       public var name : String;
       public var layers : List<Layer>;
       
       public function new()
       {
       }
    }
  3. Now, let's create a function to create a page from an XML file. Add the following function to the 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;
    }
  4. As you can see, it is not yet complete. We have to parse a layer from the XML file, so let's do it now. Add the following function in the Layer class:

    public static function fromXMLNode(node : Xml)
    {
       var nLayer : Layer;
       nLayer = new Layer...