How to write into XML file in Haxe?

1.5k views Asked by At

I am using Haxe and OpenFL and I got a program that generates a Xml file.

However, I can't figure out how to save that file. I can create Xml tree and check it's valid, but for my life I can't figure out how to write the file.

So, in simple, how to do I write(and create) a file in Haxe? I want to be able to save my newly created Xml File (they serve as sort of settings file and initialization files for my program) on computer, so that I can load it later?

1

There are 1 answers

3
Mandemon On BEST ANSWER

Found the solution right after writing this question.

Solution is to first to use sys.io.File.write() to create the file, then File.saveContent() to save the data in. You can get string from Xml with toString function, the ultimate solution looks like this:

    if (!FileSystem.exists("maps"))
    {
        FileSystem.createDirectory("maps");
    }
    var number:Int = 1;
    if (FileSystem.exists("maps/" + filename + ".xml"))
    {
        while(FileSystem.exists("maps/" + filename + number +".xml"))
        {
            number++;
        }

        filename = filename + number;
    }


    File.write("maps/" + filename + ".xml", false);
    File.saveContent("maps/" + filename + ".xml", root.toString());

This checks if the directory exist and if not, create it and if the file exist, create a new numbered file rather than override it (for the moment, still working on the save feature)

This solution only works on c++, haven't tested others much yet but Flash does not work