falcong / pugixml

Automatically exported from code.google.com/p/pugixml
0 stars 0 forks source link

the ability to query the number of children an xml_node has. #105

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What feature do you require in pugixml?
A num_children() function.  You can either call it without arguments to get all 
children, or call it with an element/tag name to get the number of children of 
a specific type.

What do you need this feature for?
Simple things like conversion from xml description to some binary format.  So 
it is nice to know how many things to allocate prior to iterating through the 
xml_node's children

If you already have a patch, please attach it to the issue.
I've done this in the past, but it was a quick hack to add the interface... 
seems like with more intimate knowledge of everything it could be O(1) rather 
than O(n).

   int xml_node::num_children() const
   {
      int num = 0;
      for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
         num++;

      return num;
   }

   int xml_node::num_children(const char* name) const
   {
      int num = 0;
      for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
         if (i->name && !strcmp(name, i->name)) num++;

      return num;
   }

Original issue reported on code.google.com by VirtualT...@gmail.com on 18 May 2011 at 9:17

GoogleCodeExporter commented 9 years ago
Unfortunately, it can't be made O(1) with the existing data structures.

Also, providing the num_children functionality without the indexing 
functionality makes it very asymmetrical; having them together, however, 
encourages people to iterate through nodes in a inefficient way.

I'll think about it further and update the issue once I reach a decision.

Original comment by arseny.k...@gmail.com on 22 Jul 2011 at 5:24

GoogleCodeExporter commented 9 years ago
When I first posted this I hadn't really discovered your xpath stuff yet.  In 
the places were I wanted to perform an iteration operation I simply do an xpath 
query.  And in general when I do any xml to binary conversions I plan on 
iterating over the query results.  Another thing to note is that those query 
results allow you to ask about the size() or number of items found by the query 
which is exactly what I needed.  Certainly having a O(1) operation for 
num_children would be nice, but the xpath interface actually provides 
everything I needed, it just took me a while to discover it.  Perhaps a small 
blurb/example in the documentation about figuring out how many children a given 
node has would be worth something.

Original comment by VirtualT...@gmail.com on 22 Jul 2011 at 12:16

GoogleCodeExporter commented 9 years ago
I'll mention this in documentation once this question comes up again; for now 
there does not seem to be a natural place where to put it (especially since 
there are different ways to do it - with STL algorithms & node iterators the 
easiest one is probably std::distance(node.begin(), node.end()))

Original comment by arseny.k...@gmail.com on 8 Mar 2012 at 7:02