ValueTree -> PropertySet -> ValueTree?


#1

I have a valueTree that contains a load of project properties. Thanks to some very elegant methods I can easily write these properties to a settings files using:

ScopedPointer<XmlElement> data (propertyValueTree.createXml());
getUserSettings()->setValue ("PROJECT_SETTINGS", data); 

However, I can’t seem to load the properties from my settings file into a new ValueTree? I thought that after I call setValue() I might be able to do something like this:

	ScopedPointer<XmlElement> xmlData = getUserSettings()->createXml("PROJECT_SETTINGS");
	newValueTree.fromXml(*xmlData);
	XmlElement * el = newValueTree.createXml();
	el->writeToFile(File("/home/rory/Desktop/Example6.xml"), String::empty);
	delete el;

But it seems that newValueTree doesn’t contain any of the data that is stored in my settings file? Any ideas what I might be doing wrong? If it’s not obvious I can prepare a simple demo.


#2

fromXml is static method which returns a ValueTree

so you need

newValueTree = ValueTree::fromXml(*xmlData);


#3

Thanks, that was a silly mistake to make! That has got me further, but there is a big difference between the xml that is created using getUserSettings()->setValue() and ValueTree::fromXml()? For example, I would have thought the following code would produce files there were more or less the same:

//write properties to settings file..
ScopedPointer<XmlElement> data (defaultValueTree.createXml());
getUserSettings()->setValue ("PROJECT_DEFAULT_SETTINGS", data);
//write new xml settings files based on data from user settings file, but using ValueTree
ScopedPointer<XmlElement> xmlData = getUserSettings()->createXml("PROPERTIES");
ValueTree newValueTree = ValueTree::fromXml(*xmlData);
XmlElement * el = newValueTree.createXml();
el->writeToFile(File("/home/rory/Desktop/NewSettings.xml"), String::empty);
delete el;	

Maybe I’m making too many assumptions about how these two classes can work together?


#4

Here’s more complete code. I wrongly expected to be able to query the new value tree for entries made to the properties file:

    appProperties = new ApplicationProperties();
    appProperties->setStorageParameters (options);
    defaultPropSet = new PropertySet();

    ValueTree defaultValueTree("Settings");
    defaultValueTree.addChild(ValueTree("Misc"), -1, 0);
    defaultValueTree.getChildWithName("Misc").setProperty("MiscSettings1", "Something1", 0);
    defaultValueTree.getChildWithName("Misc").setProperty("MiscSettings2", "Something2", 0);
    ScopedPointer<XmlElement> data (defaultValueTree.createXml());
    appProperties->getUserSettings()->setValue ("PROJECT_DEFAULT_SETTINGS", data);

    ScopedPointer<XmlElement> xmlData = appProperties->getUserSettings()->createXml("Colours");
    ValueTree newValueTree = ValueTree::fromXml(*xmlData);
    
    Logger::writeToLog(newValueTree.getChildWithName("Misc").getProperty("MiscSettings1"));

But newValueTree.getChildWithName("Misc").getProperty("MiscSettings1") returns nothing?


#5

Sorry to bump, but I’m still struggling with this. I just need to know if this should work, i.e, can I write ValueTree data as xml to a properties file, and then read the data back as xml to a new ValueTree object? If not I’ll simply try a different approach.


#6

I don’t think you will be able to get this to work. ApplicationProperties are simply not in the format of a ValueTree. But it’s definitely something we should add to JUCE: some option to tell ApplicationProperties to save the properties as a ValueTree.

I’m afraid the easiest solution is to not use ApplicationProperties and to simply save/load the ValueTree. And maybe adding listeners to save the tree when things change.


#7

Thanks Fabian. That’s all I needed to know. I think I’m developing feeling for ValueTrees…:confused: