" /> " /> "/>

Iterate whole XML file using TinyXML

479 Views Asked by At

I have some XML file's but there is one problem. My XML "tree" doesn't have one look. I mean once it can look like:

<one>
 <two>
  <three></three>
 </two>
 <four>
  <five>
   <six></six>
  </five>
  <seven></seven>
 </four>
</one>

But second time it can looks different. Is there the way with TinyXML to iterate whole file and cout like:

three: text

six: text

seven: text

I saw examples like "I have to search element by his name", but if I don't know the name, what's the solution?

I'm asking, because I have already wrote program that do that, but only goes like NextSiblingElement() and goes like first child, first child, and when it goes to "last child of parent" then it checks if that child has a brother and then it stops. So imagine that on example: it goes from "one" to "two" from "two" to "three" "three hasn't child, so it checks if it has brother. No? then stops and doesn't go to four, five etc. Where is my mistake?

TiXmlNode* element = doc.FirstChildElement();
TiXmlNode* element_parent = doc.FirstChildElement();
element = element_parent;

while (element != NULL) {
    std::cout << element->Value() << std::endl;

    if (element->FirstChildElement() == NULL) {
        element = element->NextSibling();
    }
    else {
        element = element->FirstChildElement();
    }

    if (element == NULL) {
        element = element_parent->NextSibling();
        element_parent = element_parent->NextSibling();
    }
}
0

There are 0 best solutions below