[Solved] Using the XmlElement::writeTo() method

I have searched a ton for an example of someone using the XmlElement::writeTo() method, but I can’t find anything on how to use it. My main problem is that I don’t know what format to put the parameters in, but this is what I have so far, I am sure it is incorrect.

data->XmlElement::writeTo("/Users/ethan/TFX/Resources/TableData.xml", XmlElement::TextFormat());

That seems fine to me. What’s the problem exactly, does this not work? I assume you can also do this:

data->XmlElement::writeTo("/Users/ethan/TFX/Resources/TableData.xml", {});

Yeah, what you are supposed to say is:

data->XmlElement::writeTo(juce::File("/Users/ethan/TFX/Resources/TableData.xml"), XmlElement::TextFormat());

Why are you using data->XmlElement::writeTo out of interest? Assuming data is a pointer to an XmlElement object you can just write data->writeTo.

Ha, I was so busy looking at the XmlElement::TextFormat parameter I never noticed the lack of any File object. Sorry. I wasn’t much help there :laughing:

You can supply a string literal or any other type that’s convertible to String as the first parameter because the File class has a non-explicit constructor that takes a String (meaning you can just do data->writeTo ("/My/File/Path") instead of data->writeTo (juce::File ("/My/File/Path")).

The second parameter can actually be left out entirely, since it has a default value which is an empty TextFormat object.

I personally prefer the data->writeTo style over data->XmlElement::writeTo, since it should be trivial to find out that data is an XmlElement type. This is also the more common style, in my experience.

Agreed. It does seems better.

p.s. and if I used XML any more I would probably use it a lot!