Problem reading ValueTree in from saved XML file


#1

I’m trying to read in some saved settings from xml into a ValueTree. The saved file looks fine, but when I try and load the file, the resulting ValueTree XML only contains an empty main element with no attributes.

The first version of my file to xml to ValueTree code:

    File settingsFile (settingsPath);
    XmlDocument settingsDocument (settingsFile);
    ScopedPointer<XmlElement> xml (XmlDocument::parse (settingsFile));

    if (settingsFile.existsAsFile() && xml != nullptr)
    {
        DBG ("Loading settings from " << settingsFile.getFullPathName());
        valueTree.fromXml(*xml);
        DBG (valueTree.toXmlString());
    }

then I tried using ValueTree::readFromStream but with the same result:

FileInputStream is(settingsFile);
valueTree = ValueTree::readFromStream(is);
DBG ("Loading from file " << settingsFile.getFullPathName());
DBG (valueTree.toXmlString());

Is there something obvious I’m missing here?


#2

readFromStream appears to be only for the binary format.

/** Reloads a tree from a stream that was written with writeToStream(). */
static ValueTree readFromStream (InputStream& input);

Where writeToStream is:

/** Stores this tree (and all its children) in a binary format.

    Once written, the data can be read back with readFromStream().
    ... */
void writeToStream (OutputStream& output) const;

#3

Ah I see, thanks, I want this to be user-editable so I guess my first attempt is more correct.


#4

Strangely, I can get the correct number of child elements, and grab the attributes via the XmlElement. So not sure why its failing to be read into a ValueTree?

The file is being saved by

valueTree.createXml()->writeToFile(settingsFile, String::empty);

#5

Try
valueTree = ValueTree::fromXml(*xml);

In your first version you called it as an instance function. It’s a static function that returns a valuetree.


#6

That was it, thanks, seems obvious now!