Menu

Knowing how deep in the tree i am?

2005-03-03
2013-05-20
  • Fredrik Brännbacka

    void walkScript(TiXmlElement* element)
    {
        TiXmlElement* child = 0;
        for( child = element->FirstChildElement(); child; child = child->NextSiblingElement() )
        {
            string tag = child->Value();
            printf("%s\n",child->Value());       
            walkScript(child->ToElement());
        }
    }

    Hi i have a function that looks something like this to walk all nodes.
    But i need to know how deep i am, like this:

    <tag> depth 0
        <tag> depth 1
            <tag></tag> depth 2
        </tag> depth 1
    </tag> depth 0

    Thanks In Advance

    /Fredrik

     
    • Ellers

      Ellers - 2005-03-04

      Why not do the standard recursive strategy of passing in a variable to walkScript?

      like

      void walkScript(TiXmlElement* element, int depth=0) 

      TiXmlElement* child = 0;
      for( child = element->FirstChildElement(); child; child = child->NextSiblingElement() )
      {
      string tag = child->Value();
      printf("%s Depth %d\n",child->Value(), depth); 
      walkScript(child->ToElement(), depth+1);
      }
      }

      ?

       
    • Fredrik Brännbacka

      Thank you!
      Don't know what i was thinking ; )

       
    • Ellers

      Ellers - 2005-03-07

      No worries.
      We all have those moments :)

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.