Re: [xml] libxml2 xmlNode management



On Wed, Apr 27, 2011 at 03:55:29PM +0100, Alex Bligh wrote:
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.

  Okay I tried to raise the set of problems which can come when moving
nodes around in a previous post:

  http://mail.gnome.org/archives/xml/2010-August/msg00013.html


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.

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

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.

Daniel

-- 
Daniel Veillard      | libxml Gnome XML XSLT toolkit  http://xmlsoft.org/
daniel veillard com  | Rpmfind RPM search engine http://rpmfind.net/
http://veillard.com/ | virtualization library  http://libvirt.org/



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