[sword-devel] Understanding the structure of generic books and their relationship with TreeKey.

Matthew Talbert ransom1982 at gmail.com
Mon Nov 2 22:04:36 MST 2009


> The code i was using already made use of those methods. However, i could go down no further than the root level. I am wondering how to obtain the children for the keys i've only been able to get. Here's is the current, relevant part of the code i am using:
> //
> {
>        QList<DSword::TreeNode> sectionsList;
>        sword::TreeKey *tk = dynamic_cast<sword::TreeKey*>(swModule->getKey());
>        if (tk->firstChild())
>        {
>                while (tk->nextSibling())
>                {
>                        DSword::TreeNode node;
>                        node.name = tk->getText();
>                        node.parent = NULL;
>                        sectionsList << node;
>                }
>        }
>        return sectionsList;
> }
> //
>
> Let's say i load the English GenBook, "Immitation." I only get the following:
> /Foreword
> /Book 1
> /Book 2
> /Book 3
> /Book 4
> though the last four have children. Trying to figure out how to obtain the children through trial and error have so far led me no where.

Well, I haven't used the methods in question, but it looks like you
would have to continue descending via tk->firstChild until there
aren't any more, then get all the siblings, searching each one of them
for children before moving on to the next sibling, then back up to the
parent. So basically you want to use some sort of recursive function.
Like perhaps this in psuedo-code:

list getSectionsList(TreeKey tk)
{
      list.append (tk->getText());
      if (tk->hasChildren())
      {
          tk->firstChild();
          list = getSectionsList(tk);
      } else if tk->nextSibling()
          list = getSectionsList(tk);
      else
          return list;
}

The actual implementation will depend on how your lists behave, etc.
The list should probably be passed as an argument to the function, and
the function could return bool or something. Anyway, maybe it will get
you started.

Matthew



More information about the sword-devel mailing list