标签: ptree

如何使用C++ Boost解析JSON数组?

我有一个包含一些JSON内容的文件,如下所示:

{
  "frame":
  {
    "id": "0",
    "points":
    [
      [ "0.883", "0.553", "0" ],
      [ "0.441", "0.889", "0" ],
    ]
  },
  "frame":
  ...
}
Run Code Online (Sandbox Code Playgroud)

如何使用C++和Boost ptree解析double数组的值?

c++ json boost ptree

14
推荐指数
1
解决办法
1万
查看次数

如何从ptree异常中获取xml行号

我正在使用boost ptree来读取这样的xml文件:

ptree myTree;
... /*open xml file*/
try{
    myTree.get<string>(s);
}
catch(boost::exception const&  ex)
{
/*get useful info!*/
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用该what()函数,但它会产生错误和我刚刚发送的字符串.

有没有办法获得更多有用的信息,如xml中与呼叫相关的行号?

c++ xml boost exception ptree

8
推荐指数
1
解决办法
1849
查看次数

boost :: property_tree :: ptree的内存开销是多少

我发现boost :: property_tree :: ptree有很大的内存开销.我的估计是空的ptree大约是150个字节,并且放在ptree中的任何条目至少会增加150个字节.这使得我们无法使用包含数千个条目的树.

估计我的估计?有没有办法保持低开销?

memory tree boost properties ptree

6
推荐指数
1
解决办法
1307
查看次数

添加一个子孩子来提升 ptree

假设我的目标是以以下形式创建一个 xml:

<main>
<elements>
    <element name="elem1"><temp/>
    </element>
    <element name="elem2"><temp/>
    </element>
</elements>
</main>
Run Code Online (Sandbox Code Playgroud)

我有以下代码:

ptree pt;
pt.put("main","");

ptree temp1;
temp1.put("element","");
temp1.put("element.<xmlattr>.name","elem1");
temp1.put("element.temp");

ptree temp2;
temp2.put("element","");
temp2.put("element.<xmlattr>.name","elem2");
temp2.put("element.temp");

//temp1 represents the 1st <element>...</element>
//temp2 represents the 1st <element>...</element>

//Add temp1 and temp2 to <main> under <elements>
Run Code Online (Sandbox Code Playgroud)

我认为以下内容会起作用:

pt.add_child("main.elements",temp1);
pt.add_child("main.elements",temp2);
Run Code Online (Sandbox Code Playgroud)

但是,这会生成以下 xml:

<main>
<elements>
    <element name="elem1"><temp/>
    </element>
</elements>
<elements>
    <element name="elem2"><temp/>
    </element>
<elements>
</main>
Run Code Online (Sandbox Code Playgroud)

通过将我的 temp1 设置为以下形式,我能够获得所需的 xml 文件:

temp1.put("<xmlattr>.name","elem1");
temp1.put("temp","");
temp2.put("<xmlattr>.name","elem2");
temp2.put("temp","");
pt.add_child("main.elements.element",temp1);
pt.add_child("main.elements.element",temp2);
Run Code Online (Sandbox Code Playgroud)

有没有办法可以使用我的初始 temp1 和 temp2 节点来获得所需的 xml 结构?

c++ xml boost ptree

4
推荐指数
1
解决办法
5169
查看次数

how to get properties within subsections of a ini file with boost property tree?

I am trying to use the Boost property trees to read INIfiles containing properties within sections have a "composed" path name.

For example my INIfile looks like this:

[my.section.subsection1]
someProp1=0

[my.section.subsection2]
anotherProp=1
Run Code Online (Sandbox Code Playgroud)

I read it with the following code:

namespace pt = boost::property_tree;

pt::ptree propTree;
pt::read_ini(filePath, propTree);
boost::optional<uint32_t> someProp1 = pt.get_optional<uint32_t>("my.section.subsection1.someProp1");
Run Code Online (Sandbox Code Playgroud)

The problem is that I never get the value of someProp1...

When I iterate over the first tree level I see the the entire section name …

ini boost boost-propertytree ptree

3
推荐指数
1
解决办法
5813
查看次数

使用 Boost 更好的 XML 格式?

我正在使用 Boost 属性树将我的类实例导出为 XML 节点。它有效,但它只是将所有内容放在 1 行中。我希望它有缩进,例如:

<?xml version="1.0" encoding="utf-8"?>
<root>
<sensorconfigurations>
    <configuration>
        <name>SensorConfiguration1</name>
        <sensorid>1</sensorid>
        <signalindex>1</signalindex>
        <mappingscheme>mappingscheme1</mappingscheme>
        <soundpack>test1.wav</soundpack>
    </configuration>
    <configuration>
        <name>SensorConfiguration2</name>
        <sensorid>2</sensorid>
        <signalindex>2</signalindex>
        <mappingscheme>mappingscheme1</mappingscheme>
        <soundpack>test2.wav</soundpack>
    </configuration>
    <configuration>
        <name>SensorConfiguration3</name>
        <sensorid>3</sensorid>
        <signalindex>3</signalindex>
        <mappingscheme>mappingscheme2</mappingscheme>
        <soundpack>test3.wav</soundpack>
    </configuration>
</sensorconfigurations>
</root>
Run Code Online (Sandbox Code Playgroud)

这有可能吗?我是否缺少 write_xml 方法中的参数?

这是我的代码:

void SensorConfigurationBank::save()
{
using boost::property_tree::ptree;
ptree pt;
for(map<string, SensorConfiguration>:: iterator it = sensorConfigurations_.begin(); it != sensorConfigurations_.end(); ++it) 
{
    ptree myTree;
    myTree.put("name", it->second.getName());
    myTree.put("sensorid", it->second.getSensorID());
    myTree.put("signalindex", it->second.getsignalIndex());
    MappingScheme myScheme = it->second.getMScheme();
    myTree.put("mappingscheme", myScheme.getId());
    SoundPack mySound = it->second.getSound();
    myTree.put("soundpack", mySound.filepath_);

    pt.add_child("root.sensorconfigurations.configuration", myTree);
} …
Run Code Online (Sandbox Code Playgroud)

c++ xml boost ptree

1
推荐指数
1
解决办法
589
查看次数

标签 统计

boost ×6

ptree ×6

c++ ×4

xml ×3

boost-propertytree ×1

exception ×1

ini ×1

json ×1

memory ×1

properties ×1

tree ×1