c++ rapidxml node_iterator example?
Asked Answered
K

3

6

I just started using rapidXML since it was recommended to me. Right now to iterate over multiple siblings i do this:

//get the first texture node    
xml_node<>* texNode = rootNode->first_node("Texture");
if(texNode != 0){
    string test = texNode->first_attribute("path")->value();
    cout << test << endl;
}
//get all its siblings
while(texNode->next_sibling() != 0){
    string test = texNode->first_attribute("path")->value();
    cout << test << endl;
    texNode = texNode->next_sibling();
}

as a basic test and it works fine. Anyways, I came across node_iterator which seems to be an extra iterator class to do this for me. anyways, I could not find any example on how to use it, so I was wondering if some one could show me :)

thanks!

Korns answered 20/1, 2010 at 20:2 Comment(2)
be careful about rapidxml as it doesn't do any meaningful well-formedness checking ...Freya
The above example is the right way to do it. Except if you test for texNode->next_sibling() != 0 at the beginning of your while loop, then when you get to the last sibling and set texNode = texNode->next_sibling(); at the end of the while loop , that textNode will not have a next_sibling since it is the last one. Therefor the while loop will never process the very last sibling because of that beginning check.Virtually
O
3

The documentation that I could find documents no node_iterator type. I can't even find the word iterator on that page except in reference to output iterators, which you clearly don't want.

It could be that it's an internal API, or one under development, so you're probably best not to use it right now.

Overarch answered 20/1, 2010 at 22:28 Comment(1)
yeah, thats what I figgured, if I include the iterator.hpp that comes with it it throws quite a few errors on GCC 4.2 on a mac anyways.Korns
S
3
#include "rapidxml/rapidxml.hpp"
#include "rapidxml/rapidxml_utils.hpp"
#include "rapidxml/rapidxml_iterators.hpp"

...

rapidxml::xml_document<wchar_t> doc;
doc.parse<0>(xmlFile.data());

rapidxml::node_iterator< wchar_t > begIt( doc.first_node());
rapidxml::node_iterator< wchar_t > endIt;

...

std::for_each( begIt, endIt, [] (rapidxml::xml_node< wchar_t >& node)
{
    std::wcout << node.name() << std::endl;
} );
Suchta answered 16/7, 2014 at 21:9 Comment(1)
spews out a lot of errors if I include the rapidxml_iterators.hppTransude
M
1

No RapidXml does not provide any iterator. Your solution is the one to be followed... manually using function first_node, next_sibling and so on... RapidXml is made to be light and fast... Even if a programmer would sure appreciate some syntax to help him :)

Mungovan answered 13/1, 2011 at 8:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.