Re: [xml] libxml2 xmlNode management



Daniel,

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).

  no in general it's more complex in the presence of namespace, entities
and if using document dictionaries.

Thanks. On this occasion, I can guarantee there are no namespaces,
there is no DTD (and hence no entities) and no dictionary. The
tree consists solely of node elements and attributes.

I am doing an xmlUnlinkNode() from the current document before
doing (e.g.) an AddChild(). Is this safe in this case?

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?

just from the parent and siblings, it will still reference the document,
namespaces, entities and possibly use the document dictionary

Thanks.

I resorted to reading the code at one point. It does at least appear
that you are meant to be able to link in nodes to a different document.

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?

  Unfortunately the tree calls are not fool-proof, you really need
to understand the document structure to do copy paste of subtrees.
The problem is that it's easy to made on simple document, but suddenly
if the document start using namespace or entities the application may
fail brutally.

Fortunately I can guarantee the document is simple. I am now following
using the following heuristics:
* Commands to add stuff from a tree should be done with nodes without
 parents
* Commands to remove stuff from a tree should be done with nodes with
 parents
* UnlinkNode removes things from the tree
* AddChild / ReplaceNode adds things to the tree
* Provided the document is simple (see above), it's safe to move nodes
 between documents.
* Nothing will automatically xmlFreeNode for you. So if you set the
 root element (for example) to something else, or xmlReplaceNode
 a node, the old root / replaced node is unlinked, but not freed.

Does that sound fair?

--
Alex Bligh



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