[libxml++] Node: Add get_first_child().



commit e2e324bdf798ebd93591918b1d9b2a28251c13a3
Author: Murray Cumming <murrayc murrayc com>
Date:   Mon Mar 19 12:56:51 2012 +0100

    Node: Add get_first_child().
    
    * libxml++/nodes/node.[h|cc]: This is like get_children(),
    but it returns only the first node, optionally returning
    the first one with a certain name.
    Based on a patch by Ilya Murav'jov in bug #648125 .

 ChangeLog              |    9 +++++++++
 libxml++/nodes/node.cc |   37 +++++++++++++++++++++++++++++++++----
 libxml++/nodes/node.h  |   14 ++++++++++++++
 3 files changed, 56 insertions(+), 4 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 7be3967..39e5013 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2012-03-19  Murray Cumming  <murrayc murrayc com>
+
+	Node: Add get_first_child().
+
+	* libxml++/nodes/node.[h|cc]: This is like get_children(),
+	but it returns only the first node, optionally returning
+	the first one with a certain name.
+	Based on a patch by Ilya Murav'jov in bug #648125 .
+
 2.35.1:
 
 2012-02-15  Kjell Ahlstedt  <kjell ahlstedt bredband net>
diff --git a/libxml++/nodes/node.cc b/libxml++/nodes/node.cc
index 6ce2b64..5face47 100644
--- a/libxml++/nodes/node.cc
+++ b/libxml++/nodes/node.cc
@@ -76,6 +76,38 @@ Node* Node::get_previous_sibling()
   return static_cast<Node*>(cobj()->prev->_private);
 }
 
+static Node* _convert_node(xmlNode* node)
+{
+  Node* res = 0;
+  if(node)
+  {
+    Node::create_wrapper(node);
+    res = static_cast<Node*>(node->_private);
+  }
+  return res;
+}
+
+Node* Node::get_first_child(const Glib::ustring& name)
+{
+  xmlNode* child = impl_->children;
+  if(!child)
+    return 0;
+
+  do
+  {
+    if(name.empty() || name == (const char*)child->name)
+      return _convert_node(child);
+  }
+  while((child = child->next));
+   
+  return 0;
+}
+
+const Node* Node::get_first_child(const Glib::ustring& name) const
+{
+  return const_cast<Node*>(this)->get_first_child();
+}
+
 Node::NodeList Node::get_children(const Glib::ustring& name)
 {
    xmlNode* child = impl_->children;
@@ -86,10 +118,7 @@ Node::NodeList Node::get_children(const Glib::ustring& name)
    do
    {
       if(name.empty() || name == (const char*)child->name)
-      {
-        Node::create_wrapper(child);
-        children.push_back(reinterpret_cast<Node*>(child->_private));
-      }
+        children.push_back(_convert_node(child));
    }
    while((child = child->next));
    
diff --git a/libxml++/nodes/node.h b/libxml++/nodes/node.h
index 9b8e909..06d2753 100644
--- a/libxml++/nodes/node.h
+++ b/libxml++/nodes/node.h
@@ -96,6 +96,20 @@ public:
    */
   Node* get_previous_sibling();  
 
+  /** Get the first child of this node. You may optionally get the first child node which has a certain name.
+   * @returns The first child
+   *
+   * @newin{2,36}
+   */
+  const Node* get_first_child(const Glib::ustring& name = Glib::ustring()) const;
+
+  /** Get the first child of this node. You may optionally get the first child node which has a certain name.
+   * @returns The first child
+   *
+   * @newin{2,36}
+   */
+  Node* get_first_child(const Glib::ustring& name = Glib::ustring());
+
   /** Obtain the list of child nodes. You may optionally obtain a list of only the child nodes which have a certain name.
    * @param name The names of the child nodes to get. If you do not specigy a name, then the list will contain all nodes, regardless of their names.
    * @returns The list of child nodes.



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