Pregunta

Estoy usando boost::property_tree. La documentación es muy vaga y poco útil en general en su mayor parte. En cuanto a la fuente / ejemplos no ayudó mucho, tampoco.

Lo que me pregunto es la siguiente:

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

¿Cómo puedo iterar sobre todos los elementos en el nivel actual? Si hago esto:

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

En ParseEntry que intento esto:

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

Los resultados en una excepción (no no existe). También he intentado esto:

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

El mismo problema.

A mi entender cuando llamo ParseEntry estoy pasando una referencia al árbol en ese nodo.

¿Hay alguna manera de hacer frente a esto, cuando tengo varias entradas de StringTable usando árbol de propiedades?

¿Fue útil?

Solución

ParseEntry recibe una referencia a cada uno de los nodos hijos del nivel actual. Por lo tanto, no se puede pedir a los valores utilizando el nombre de nodo, porque ya tiene un nodo secundario. El nombre de nodo se almacena en v.first .

Se puede iterar sobre todos los elementos de un nivel determinado, utilizando get_child para seleccionar el nivel y luego BOOST_FOREACH para iterar. Cada iterador habrá un par que representa el nombre del nodo y los datos del nodo:

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

Este código imprimiría:

  

Nombre: Idioma, Valor: ES

     

Nombre: DataPath, Valor: \\ .. \\ Datos de Recursos \\ \\ Cuerdas stringtable.bst

Si no desea iterar, se puede seleccionar el nivel de nodo y luego buscar los nodos usando su nombre:

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;

Este código imprimiría:

  

en

     

.. \\ \\ Datos de Recursos \\ \\ Cuerdas stringtable.bst

Otros consejos

No he utilizado el árbol de propiedades, pero probablemente lo hará, ya que parece ingenioso. Algunas observaciones rápidas sin embargo:

No debería el parámetro de plantilla de conseguir ser el mismo que el tipo de cambio?

VGHL :: string langname = stringTree.get (...);

Pero esto es más probable no es un problema aquí, ya que esto habría dado lugar a error en tiempo de compilación.

No estoy seguro si L "VGHL.StringTable.Language" obras de argumentos?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top