A few questions about XmlElement


Hi all:

I am creating an initialization file for my upcoming plugin. This file will allow the user to specify a path to a keyfile (currently by calling FileChooser), and may copy the data from the keyfile over - I haven’t decided yet.

Anyway, the initialization file is XML. I was planning on using an element for the path to the keyfile. For whatever reason, when I create a local XmlElement, store the path as an attribute, and then read out this attribute, the path can be read as a properly formatted String. However, if I try to use an element, via setText() and getAllSubText(), the path shows up as blank. Any idea what is going on here?

Also, I had planned on using writeToFile() once I have added the path to the original XmlElement that I derived from my XML initialization file. There is an argument for which DTD to use. My XML file is pretty simple, and not based on any schema, so what should I put in there? Can I leave this blank, or just put in some dummy schema? If not, what should I do?


Sean Costello


Text elements are fundamentally different from attributes - you can’t just treat them as key/value pairs like you can with attributes (although a lot of XML is written in that style). To make it easier to use them like that, I added the createTextElement() and getChildElementAllSubText() utility methods, so you can do things like:

element.createTextElement ("foo").setText ("bar"); element.getChildElementAllSubText ("foo", "") // returns "bar"

Yes, you can happily leave the DTD blank, I always do that.


I tried this code (same as what you wrote, but with an XmlElement declared):

XmlElement element(T("demo"));
element.createTextElement ("foo").setText ("bar");
element.getChildElementAllSubText ("foo", ""); // returns "bar"

When I try to build, Xcode gives me the following error:

Same thing happens if I change the .setText() parameter to T(“bar”).

Any ideas what might be going on? Is this something that has been changed in the last few months?


Sean Costello


And, while I’m here, do you think that using attributes for my path (and serial number/keyfile) would be a better idea than elements? I was wanting a document that makes sense when I read it, and I have always used elements in my XML, but if attributes make more sense for Juce, that is fine with me.


Sean Costello


Sorry, I obviously just typed “.” when it should have been “->”

If it doesn’t matter what your xml looks like then yes, attributes are easier. Or use a ValueTree, or even a PropertiesFile instead, which all get saved as xml anyway.


I switched to using attributes. For now, that will work fine.