[xml] libxml2 xmlNode management

Apologies in advance for the newbie question, but having read the tutorials
and I think the relevant pages of the documentation, I haven't been able
to find answers to this, perhaps because my application is different
from many.

Most libxml2 examples (and possibly most usages) envisage an xml file being
parsed, and then the document destroyed. Provided everything is destroyed
when the document is destroyed, people are happy. My proposed use is to
use the xmlDocument structure throughout the life a long running daemon
process, and to prune on and off various new bits into the XML tree. This
is in essence because the daemon is passed bits of XML (without DTD,
therefore incorporating bits it can regard as opaque). Efficient memory
management is therefore important to me.

So, questions:

1. Can an xmlNode that has children in document tree be "pruned" from
  one document and added to another together with its children? Is this
  a simple matter of doing an xmlAddChild? (ie are operations like
  xmlAddChild safe with xmlNodes that already form part of the same
  or a different document).

2. xmlNodes can be created without adding them to an xmlDocument.
  Can such an xmlNode have children? If so how do I prune a subtree
  of xmlNodes off an existing tree without adding them to a new
  one (i.e. how do I reverse xmlAddChild)? I think this is
  xmlUnlinkNode though the documentation refers to this as
  unlinking it from its "current context". Does that mean it is
  unlinked from just its parent? Or its children too?

3. If I want to prune a node and its children and delete them
  how can I do that without risking a memory leak? Does xmlUnlinkNode
  followed by xmlFreeNode (which is described as recursive) do that. If the
  node is unlinked, how can it have children?

4. Is it safe to xmlFreeNode a node which is part of a tree?

I get the feeling some calls (e.g. xmlAddChild) will deal intelligently
with the node's existing parents or children, but some (perhaps e.g.
xmlFreeNode) will not. Is there a list somewhere, or perhaps better
an idiots guide to how these tree manipulation operations work?

Alex Bligh

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]