I’m using the excellent PropertySet class, and just added an XML value to it, but I see that when written to a file, it comes out quite mangled. Of course, it reads itself well, but it misses the best feature of both that class (as XML) and of XML itself - the human readable and editable aspect.
Is that something that could be altered? Currently I get:
Note that an XML editor also can’t open the current version. It doesn’t seem like much would be broken by this, since the PropertySet only looks for tags in the root element anyway.
Well, I’ll probably do it here then and then change to your version when you have it.
Any suggestion on how to get to the structure I defined? Is it acceptable to pass the XML elements in a donor XmlElement, or do you think the VALUE element will have to hold the incoming element:
I’d have thought it would just set/get a single xml node, which would go inside the value node. It’s up to your app what the structure of that node would be.
stopTimer();
if (file == File::nonexistent
|| file.isDirectory()
|| ! file.getParentDirectory().createDirectory())
return false;
if ((options & storeAsXML) != 0)
{
XmlElement* const doc = new XmlElement (propertyFileXmlTag);
for (int i = 0; i < getAllProperties().size(); ++i)
{
XmlElement* const e = new XmlElement (propertyTagName);
e->setAttribute (T("name"), getAllProperties().getAllKeys() [i]);
// Can the value be best expressed as XML?
XmlDocument xmlVal (getAllProperties().getAllValues()[i]);
if (xmlVal.getDocumentElement(true))
{
e->addChildElement(new XmlElement(*xmlVal.getDocumentElement()));
}
else
e->setAttribute (T("val"), getAllProperties().getAllValues() [i]);
doc->addChildElement (e);
}
const bool ok = doc->writeToFile (file, String::empty);
[/code]
and in the constructor…[code] if (doc != 0 && doc->hasTagName (propertyFileXmlTag))
{
delete doc;
doc = parser.getDocumentElement();
if (doc != 0)
{
forEachXmlChildElementWithTagName (*doc, e, propertyTagName)
{
const String name (e->getStringAttribute (T("name")));
if (name.isNotEmpty())
{
if (e->hasAttribute (T("val")))
{
getAllProperties().set (name, e->getStringAttribute (T("val")));
}
else if (e->getNumChildElements() == 1)
{
getAllProperties().set (name, e->getChildElement(0)->createDocument (String::empty, true));
}
}
}
}
else
{
// must be a pretty broken XML file we're trying to parse here!
jassertfalse
}
delete doc;
}