ValueTree.fromXml doesn't work

Hi,

I wrote this code:

    root1.addChild(child1, -1, nullptr);
    root1.addChild(child2, -1, nullptr);
    child1.addChild(child11, -1, nullptr);
    child2.addChild(child21, -1, nullptr);
    child2.addChild(child22, -1, nullptr);
    XmlElement screenshot = *root1.createXml().get();
    DBG("=====================");
    DBG(screenshot.toString());
    root2.fromXml(screenshot);
    DBG("=====================qui");
    DBG(root2.toXmlString());

and I expect this in console:

=====================
<?xml version="1.0" encoding="UTF-8"?>

<root>
  <child1>
    <child11/>
  </child1>
  <child2>
    <child21/>
    <child22/>
  </child2>
</root>

=====================
<?xml version="1.0" encoding="UTF-8"?>

<root>
  <child1>
    <child11/>
  </child1>
  <child2>
    <child21/>
    <child22/>
  </child2>
</root>

while it prints this:

=====================
<?xml version="1.0" encoding="UTF-8"?>

<root>
  <child1>
    <child11/>
  </child1>
  <child2>
    <child21/>
    <child22/>
  </child2>
</root>

=====================qui
<?xml version="1.0" encoding="UTF-8"?>

<root/>

what is missing from me?

fromXml is a static function that returns a ValueTree. It’s not applied to an instance. Also, your initialization of screenshot is making a deep copy. To use the move constructor you’d have to write

juce::XmlElement screenshot = std::move(*root1.createXml().get());

but it’s unnecessary. You can just use the unique_ptr as it is.

juce::ValueTree root1{ "root" }, child1{ "child1" }, child2{ "child2" },
                child11{ "child11" }, child21{ "child21" }, child22{ "child22" };
root1.addChild(child1, -1, nullptr);
root1.addChild(child2, -1, nullptr);
child1.addChild(child11, -1, nullptr);
child2.addChild(child21, -1, nullptr);
child2.addChild(child22, -1, nullptr);
auto screenshot = root1.createXml();
DBG("=====================");
DBG(screenshot->toString());
auto root2 = juce::ValueTree::fromXml(*screenshot.get());
DBG("=====================qui");
DBG(root2.toXmlString());
1 Like

ok, clear, so sorry, I miss that it was a static func :pray: