如何将Boost属性树的子树序列化为XML

How to serialize a subtree of a Boost property tree to XML

本文关键字:序列化 XML Boost 属性      更新时间:2023-10-16

我正在使用boost c 属性树库(https://wwwww.boost.org/doc/libs/1_70_0_0/doc/doc/html/property_tree.html(来读取和综述一些简单的xml。

首先,我是从XML文档构建属性树 - 效果很好。

但是,我无法获得对子节点的引用(例如,如下示例中的" b1"(,然后序列化整个root是" b1"的子树,包括" b1",一个新的XML文档。

我尝试使用get_child,但这只会序列化我子树的根节点的孩子(也许是根据其名称进行的(。

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>
namespace pt = boost::property_tree;
void test() {
    pt::ptree tree;
    // populate tree
    tree.put("a.b1", "value1");
    tree.put("a.b2", "value2");
    tree.put("a.b1.c1", "");
    tree.put("a.b1.c2", "");
    tree.put("a.b1.c3", "");
    tree.put("a.b1.c4", "");
    std::ostringstream os;
    pt::write_xml(os, tree);
    // 1. dump tree as xml to cout
    std::cout << os.str() << std::endl;
/*
    here I would like to use some sort of get_subtree 
    function get a subtree of "tree" starting at and including 
    node "a.b1", and serialize it as XML:
    pt::tree subtree = tree.get_subtree( "a.b1" );
    std::ostringstream os1;
    pt::write_xml(os1, subtree)
    // 2. dump subtree as xml to cout
    std::cout << os1.str() << std::endl;
*/
}

我在1处得到的是(我的格式化,write_xml都放在一行上(

<?xml version="1.0" encoding="utf-8"?>
<a>
 <b1>value1
  <c1/>
  <c2/>
  <c3/>
  <c4/>
 </b1>
 <b2>value2
 </b2>
</a>

我想在2处获得的是以下XML,其中" B1"是新子树的根

<?xml version="1.0" encoding="utf-8"?>
<b1>value1
 <c1/>
 <c2/>
 <c3/>
 <c4/>
</b1>

这是我称为get_child(" a.b1"(的我得到的,以获取新的子树以" b1"开始(这次是原始格式(:

<?xml version="1.0" encoding="utf-8"?>
b1_value<c1/><c2/><c3/><c4/>

XML甚至都不是很好。

更新只是为了进行测试,我在原始树的根级别上添加了几个元素。这是新代码:

void test() {
        pt::ptree tree;
        // populate tree
        tree.put("a.b1", "b1_value");
        tree.put("a.b2", "b2_value");
        tree.put("a.b1.c1", "");
        tree.put("a.b1.c2", "");
        tree.put("a.b1.c3", "");
        tree.put("a.b1.c4", "");
        tree.put("x", "");
        tree.put("x.m", "");
        tree.put("y", "");
        std::ostringstream os;
        pt::write_xml(os, tree);
        // dump tree as xml to cout
        std::cout << os.str() << std::endl;;
}

和输出:

<?xml version="1.0" encoding="utf-8"?>
<a>
 <b1>b1_value
  <c1/>
  <c2/>
  <c3/>
  <c4/>
 </b1>
 <b2>b2_value</b2>
</a>
<x>
 <m/>
</x>
<y/>

这显然不是很好的XML,并且没有直接帮助我,但是它表明库在与任何级别的树时都保持一致。

更新我修改了标题以正确反映我实际要做的事情 - 我不仅仅是想获得子树,而且还将其序列化为XML。我遇到的问题是,仅使用记录的类方法,而我可以得到一个子树,序列化遍历根节点,其子女(这是我想要的(,但它的同行节点也(这是不需要的(。/p>

前奏

库仅在映射到不同序列化后端的方式上不稳定。限制在(非常简洁的(文档中很明确提及。

这很令人惊讶。

博览会

简化样本:

pt::ptree tree;
// populate tree
tree.put("a.b1", "value1");
tree.put("a.b2", "value2");
tree.put("a.b1.c1", "");
tree.put("a.b1.c2", "");
tree.put("a.b1.c3", "");
tree.put("a.b1.c4", "");
auto options = pt::xml_writer_make_settings<std::string>(' ', 4);
write_xml(std::cout, tree, options);

打印

<?xml version="1.0" encoding="utf-8"?>
<a>
    <b1>
        value1
        <c1/>
        <c2/>
        <c3/>
        <c4/>
    </b1>
    <b2>value2</b2>
</a>

获得子树

此部分很简单:

pt::ptree const& subtree = tree.get_child("a.b1");

选项1

使其打印"正确"添加Pro-Forma顶级节点:

// option 1
{
    pt::ptree fake_root;
    fake_root.add_child("b1", subtree);
    write_xml(std::cout, fake_root, options);
}

打印

<?xml version="1.0" encoding="utf-8"?>
<b1>
    value1
    <c1/>
    <c2/>
    <c3/>
    <c4/>
</b1>

选项2

如果您认为这很烦人/浪费,您可以使用无证件的内部功能来编写单个元素:

// option 2
{
    pt::xml_parser::write_xml_element(std::cout, "b1", subtree, 0, options);
}

打印

<b1>
    value1
    <c1/>
    <c2/>
    <c3/>
    <c4/>
</b1>

实时演示

活在coliru

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>
namespace pt = boost::property_tree;
int main() {
    pt::ptree tree;
    // populate tree
    tree.put("a.b1", "value1");
    tree.put("a.b2", "value2");
    tree.put("a.b1.c1", "");
    tree.put("a.b1.c2", "");
    tree.put("a.b1.c3", "");
    tree.put("a.b1.c4", "");
    auto options = pt::xml_writer_make_settings<std::string>(' ', 4);
    write_xml(std::cout, tree, options);
    /*
        here I would like to use some sort of get_subtree
        function get a subtree of "tree" starting at and including
        node "a.b1", and serialize it as XML:
    */
    pt::ptree const& subtree = tree.get_child("a.b1");
    // option 1
    {
        pt::ptree fake_root;
        fake_root.add_child("b1", subtree);
        write_xml(std::cout, fake_root, options);
    }
    // option 2
    {
        pt::xml_parser::write_xml_element(std::cout, "b1", subtree, 0, options);
    }
}