views:

1274

answers:

2

So a quick question i started using boost::property_tree, anyways. The documentation is very vague and overall unhelpful, for the most part. Looking at the source/examples didnt help that much ether.

What im wondering is the following:

<VGHL>
    <StringTable>
     <Language>EN</Language>
     <DataPath>..\\Data\\Resources\\Strings\\stringtable.bst</DataPath>
    </StringTable>
</VGHL>
  1. How can i iterate over all the elements at the current level? If i do this:

    read_xml(fin, bifPropTree);
    VGHL::String tablePath;
    BOOST_FOREACH(boost::property_tree::wiptree::value_type &v, bifPropTree.get_child(L"VGHL.StringTable"))
    {
        m_StringTable->ParseEntry(v.second, tablePath);
    }
    

In parseEntry i try this:

VGHL::String langName = stringTree.get<VGHL::String>(L"StringTable.Language");

Results in an exception(not doesnt exist), also trying this:

VGHL::String langName = stringTree.get<VGHL::String>(L"Language");

Same problem.

From my understanding when i call ParseEntry i am passing a reference to the tree at that node.

Otherwise is there any way to deal with this, when i have multiple entries of StringTable using property tree?

A: 

I've not used the property tree, but probably will as it looks nifty. A few quick observations though:

Should not the template parameter of get be the same as the return type?

VGHL::String langName = stringTree.get(...);

But this is most likely not a problem here, as this would have resulted in compile-time error.

Not sure if L"VGHL.StringTable.Language" argument works?

Amit Kumar
Thanks for pointing the that out im basically in the process of merging codebases. VGHL and Ludo::String both are typedefs of std::wstring :)That wasnt the problem :(
UberJumper
Also i just checked, that doenst work ether. You can use iterators, but they will always be on the current level.
UberJumper
+5  A: 

ParseEntry receives a reference to each of the children nodes of the current level. So, you cannot ask the values using the node name, because you already have a child node. The node name is stored in v.first.

You can iterate over all the elements at a given level using *get_child* to select the level and then *BOOST_FOREACH* to iterate. Each iterator will be a pair representing the name of the node and the node data:

using boost::property_tree::wiptree;

wiptree &iterationLevel = bifPropTree.get_child(L"VGHL.StringTable");
BOOST_FOREACH(wiptree::value_type &v, iterationLevel)
{   
  wstring name = v.first;
  wstring value = v.second.get<wstring>(L"");
  wcout << L"Name: " << name << L", Value: " << value.c_str() << endl;
}

This code would print:

Name: Language, Value: EN

Name: DataPath, Value: ..\\Data\\Resources\\Strings\\stringtable.bst

If you do not want to iterate, you can select the node level and then look for the nodes using their name:

wiptree &iterationLevel = bifPropTree.get_child(L"VGHL.StringTable");
wstring valueLang = iterationLevel.get<wstring>(L"Language");
wstring valuePath = iterationLevel.get<wstring>(L"DataPath");
wcout << valueLang << endl << valuePath << endl;

This code would print:

EN

..\\Data\\Resources\\Strings\\stringtable.bst

J. Calleja