提升属性树和XML解析问题 [英] Boost Property Tree and Xml parsing Problems

查看:68
本文介绍了提升属性树和XML解析问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的boost :: property_tree 。该文档是非常模糊和整体无益大部分。查看源/例子并没有帮助太多,无论是。

I'm using boost::property_tree. The documentation is very vague and overall unhelpful for the most part. Looking at the source/examples didn't help that much, either.

我想知道的是以下内容:

What I'm wondering is the following:

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

我如何可以遍历在目前的水平所有的元素?如果我这样做:

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

ParseEntry 我试试这个:

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

结果异常(不是不存在)。我也试过这样:

Results in an exception (not doesn't exist). I've also tried this:

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

同样的问题。

从我的理解,当我打电话 ParseEntry 我在那个节点传递到树的引用。

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

有什么办法来解决这个问题,当我有多个条目 STRINGTABLE 使用属性树?

Is there any way to deal with this, when I have multiple entries of StringTable using property tree?

推荐答案

ParseEntry接收到每个电流电平的子节点的一个参考。所以,你不能要求使用节点名的值,因为你已经有一个子节点。节点名称存储在的 v.first

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.

您可以通过重复使用在* get_child *选择级别,然后* * BOOST_FOREACH来迭代一个给定级别的所有元素。每个迭代将一对重新presenting节点的名称和节点数据

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

这code将打印:

名称:语言,值:EN

Name: Language, Value: EN

名称:数据路径值:.. \\\\ \\\\的数据资源\\\\ \\\\字符串stringtable.bst

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;

这code将打印:

EN

.. \\\\ \\\\的数据资源\\\\ \\\\字符串stringtable.bst

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

这篇关于提升属性树和XML解析问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆