Re: [xml] libxml2 xmlNode management



On 27/04/2011 16:55, Alex Bligh wrote:
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).

there is a function xmlSetTreeDoc which should do this (i've never used it):
http://xmlsoft.org/html/libxml-tree.html#xmlSetTreeDoc

i guess you also have to call xmlUnlinkNode on the original first, then
xmlSetTreeDoc, and then xmlAddChild with some node in the target document
to insert the original node.

i don't believe xmlAddChild with nodes from 2 different documents will
work; you'll probably sometimes then get nodes that are linked into both
documents.
hmmm, just had a look at xmlAddChild, actually it does this:
    if (cur->doc != parent->doc) {
        xmlSetTreeDoc(cur, parent->doc);
    }
so you don't actually have to call it yourself.

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

AFAIK nodes without a document may have children.
but i guess then the children better also have no document.

   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?

xmlUnlinkNode will unlink the node from both its parent and its siblings,
but not from its children or the document.

so xmlUnlinkNode is basically (almost) the reverse of xmlAddChild.

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?

yes, xmlUnlinkNode + xmlFreeNode should do it.
as already mentioned, xmlUnlinkNode leaves children alone.

note also that xmlFreeDoc will not free nodes that have been unlinked; the
only element nodes freed by xmlFreeDoc are those reachable from the root
element.

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

you first have to call xmlUnlinkNode.
then xmlFreeNode should be safe.

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?

i've also asked myself this question, when i debugged OOo's libxml-based
DOM implementation some months ago, which needs to do all sorts of tree
manipulation (and was often doing it wrong); mainly i just read the
implementation when the documentation wasn't clear enough :)

regards,
 michael

-- 
"If you think good architecture is expensive, try bad architecture."
 -- Brian Foote and Joseph Yoder, "Big Ball Of Mud"




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