Questions about managing XmlElement trees (copying/deleting etc...)

Hi everyone!

I have a 2 questions about XML use in JUCE,
that I haven’t found any clear information for in the manual…

I’m using XmlElements as “nodes” in a large tree structure to store all my app’s data.
The top node is called root, and it has many leaf and branch nodes.
The children of one node are simply attached as child node(s) using the XmlElements.

  1. Deleting a tree of XmlElements

If I, for example, create an XmlElement called “root”,
XmlElement* rootNode = new XmlElement(“root”); (BTW: I use pointers and allocate with “new”)
and then attach a tree of child XmlElement nodes to it,
and I delete the root node, will all the connected child nodes of the whole tree be deleted too?,
or do I have to delete them all manually ?
If it’s possible to have them all deleted automatically with the node I’m deleting, how do I do that?
Also, this would be for any node in the tree,
so if i delete a node somewhere in the tree, will all of that node’s children be deleted too,
or again, do I have to delete them all manually ?

  1. Copying a tree or parts of it.

Let’s say that I want to copy my root XmlElement with lots of child nodes attached to a new XmlElement,
with the whole tree also copied and attached.
Eg, I’d like an identical tree that is fully separated and has no links in any way
to the original/first tree.
Can I just copy the original XmlElement? and if so, how do I do it ?
Or, do I need to manually copy each element of the tree and attach it, set attributes etc… ?
Or, as in the previous point, how can I copy a part of a tree, eg an element somewhere
down in the tree, with all it’s children etc… copied and attached identically.

This way I can easily copy/paste data around and easily implement a multiple undo/redo system.

Would be cool if someone can clarify the above 2 questions for me…

Much appreciated!,
Cheers,
Terrence

The documentation of XmlElement can answer that:

addChildElement(XmlElement*):

Child elements are deleted automatically when their parent is deleted, so make sure the object that you pass in will not be deleted by anything else, and make sure it’s not already the child of another element.

removeChildElement(XmlElement *childToRemove, bool shouldDeleteTheChild):

shouldDeleteTheChild: if true, the child will be deleted, if false it’ll just remove it

I don’t see a built in method to create a deep copy, only to copy individual nodes. You would need to check the copy constructor and assignment operator if they copy child nodes, best to look at the sources.

My personal advice:
Unless you need text nodes (that’s the only difference afaik), I would strongly recommend to move to juce::ValueTree to manage and serialise XML.
The advantages:

  • it works with a lightweight stack wrapper and reference counting, so you don’t have any lifetime issues. The questions above don’t arise at all there.
  • you get a listener and undo/redo model, which makes state handling very easy
  • the API to create nodes is much simpler and safer

The XmlElement API should be rewritten to use std::unique_ptr everywhere to make ownership really clear. But I think it is not in the focus of the JUCE framework, so I wouldn’t expect it to happen. It also breaks a lot of code, so I would treat it as a legacy and not use it in new projects.

Another reason is, that the power of XML starts with schemata and grammar, so you can validate and create transformations like XSLT and all the magic.
So if you want to become an XML power user, I would look for a full feature library for XML and integrate that instead.

/** Creates a (deep) copy of another element. */
XmlElement (const XmlElement&);

/** Creates a (deep) copy of another element. */
XmlElement& operator= (const XmlElement&);

like I said…

it is not clear if that copies the tree. does it?

Yes. That’s what the “(deep)” means. They both call copyChildrenAndAttributesFrom(other). That function uses the copy constructor on each child to do a deep copy of each child.

Ok, thanks for clearing that up

Hi everyone,

Thanks a lot for clearing this up for me.
Now I know how to manage my XML tree and make deep copies and deletes…

If I run into any further issues,
I’ll ask for help here!

Thanks again :slight_smile: It’s highly appreciated!
Terrence

1 Like