7

我正在使用boost::property_tree. 该文档非常模糊,并且在大多数情况下总体上没有帮助。查看源代码/示例也没有太大帮助。

我想知道的是以下内容:

<VGHL>
    <StringTable>
        <Language>EN</Language>
        <DataPath>..\\Data\\Resources\\Strings\\stringtable.bst</DataPath>
    </StringTable>
</VGHL>

如何迭代当前级别的所有元素?如果我这样做:

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);
}

ParseEntry我尝试这个:

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

导致异常(not 不存在)。我也试过这个:

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

同样的问题。

根据我的理解,当我打电话时,ParseEntry我正在传递对该节点的树的引用。

当我有多个StringTable使用属性树的条目时,有什么办法可以解决这个问题?

4

2 回答 2

14

ParseEntry 接收对当前级别的每个子节点的引用。因此,您不能使用节点名称询问值,因为您已经有一个子节点。节点名称存储在v.first中。

您可以使用get_child来迭代给定级别的所有元素以选择级别,然后使用BOOST_FOREACH进行迭代。每个迭代器将是一对表示节点名称和节点数据的对:

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;
}

此代码将打印:

名称:语言,值:EN

名称:DataPath,值:..\\Data\\Resources\\Strings\\stringtable.bst

如果不想迭代,可以选择节点级别,然后使用节点名称查找节点:

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;

此代码将打印:

CN

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

于 2009-11-25T16:51:20.413 回答
0

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?

于 2009-11-25T04:10:17.690 回答