Good day! Please help me figure out what's wrong with recursion. It does not work correctly, and that's it! Using the recursive function, looking for a tag with a given value of a given attribute. However, the function always returns that there is no such attribute, although I know for sure that there is. From this I conclude that the recursive traversal works incorrectly. Here is the code:
int depthSearch(tree<htmlcxx::HTML::Node> const & dom, tree<htmlcxx::HTML::Node>::iterator &returnIt, tree<htmlcxx::HTML::Node>::iterator ¤tIterator, string tagName, string attributeName, string attrContent) { int errorCode = 0; //return fixed-depth iterator to the first node at a given depth for given iterator tree<htmlcxx::HTML::Node>::iterator it = dom.begin_fixed (currentIterator, 0); tree<htmlcxx::HTML::Node> currDom; for ( unsigned i = 0; i < dom.number_of_children(it); i++ ) { //return child node of current node currDom = dom.child(it, i); //Return leaf iterator to the first leaf of the subtree at the given node. currentIterator = currDom.head; if (currDom.number_of_children(it) > 0) { errorCode = depthSearch(currDom, returnIt, currentIterator, tagName, attributeName, attrContent); if (errorCode == 0) { returnIt = it; return 0; } } else { errorCode = GoToTagWithAttr(currDom.begin_fixed(currentIterator, 0), tagName, attributeName, attrContent); if (errorCode == 0) { returnIt = it; return 0; } } } return 1; } The essence of the algorithm: we go over the children of nodes, we reach the deepest, we begin to check whether there are necessary attributes in it. Further, if not, roll back recursively and check again. If you find the desired node with the desired attribute, we immediately exit and remember the pointer of the found tag.
Link to library documentation: htmlcxx documentation .
An article about the use of iterators: htmlcxx basic description and basic techniques of use .
And here are just a few links to how people use the library: