GNOME Bugzilla – Bug 351876
Implement Node::get_parent()
Last modified: 2006-11-17 15:35:18 UTC
Currently there's no way to retrieve the parent element for a node in the DOM. Attached is a patch that implements the method: Element* Node::get_parent()
Created attachment 71123 [details] [review] Implements missing method.
Thanks. It looks good to me, though it probably needs both a const and unconst version: const Element* get_parent() const; Element* get_parent(); and a ChangeLog entry. Then it should be comitted to HEAD after we have created a gnome-2-16 branch.
I would say get_next_sibling and get_previous_sibling would be good to have as well
I don't see a gnome-2-16 branch yet, but I'll attach my final patches and test program.
Created attachment 75377 [details] [review] Patch with constness, get_xxx_sibling
Created attachment 75378 [details] Tests new functionality
This seems to be accessing the struct members directly. Is there any libxml documentation that says that this is a legitimate thing to do? Even if it is, I would rather not add the sibling stuff. We already have a way to list the children of a node.
Yes, accessing the struct members directly is how you walk a tree in libxml: http://xmlsoft.org/examples/tree1.c http://xmlsoft.org/examples/tree2.c And this is how libxml++ currently works (couldn't work without it).
Committed Node::get_parent(). As far as the sibling stuff, it really does make a difference when implementing many XML algorithms. In DOM, using Node.firstChild, Node.lastChild, Node.nextSibling, Node.previousSibling and Node.parent together allow you to do many things that would otherwise be unwieldly or overly complicated. This is especially true when dealing with 'document' oriented XML. I'm the author of RTFX and made heavy use of these methods when writing it: http://memberwebs.com/nielsen/software/rtfx/
OK. Please go ahead. Thanks for convincing me. For the const overloads, you might want to do return const_cast<Node*>(this)->the_method(); just to reduce the library size slightly, though we probably don't do that elsewhere yet, and the copy/pasted implementation is small.
Thanks. Done.