添加子子项以提升ptree(Adding a sub child to boost ptree)

编程入门 行业动态 更新时间:2024-10-27 08:33:07
添加子子项以提升ptree(Adding a sub child to boost ptree)

让我们说我的目标是以下面的形式创建一个xml:

<main> <elements> <element name="elem1"><temp/> </element> <element name="elem2"><temp/> </element> </elements> </main>

我有以下代码:

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>

我认为以下方法可行:

pt.add_child("main.elements",temp1); pt.add_child("main.elements",temp2);

但是,这会生成以下xml:

<main> <elements> <element name="elem1"><temp/> </element> </elements> <elements> <element name="elem2"><temp/> </element> <elements> </main>

通过以下面的形式制作我的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);

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

Lets say I am aiming to create an xml in the following form:

<main> <elements> <element name="elem1"><temp/> </element> <element name="elem2"><temp/> </element> </elements> </main>

I have the following code:

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>

I would assume the following would work:

pt.add_child("main.elements",temp1); pt.add_child("main.elements",temp2);

However this generates the following xml:

<main> <elements> <element name="elem1"><temp/> </element> </elements> <elements> <element name="elem2"><temp/> </element> <elements> </main>

I was able to get the required xml file by making my temp1 in the following form:

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

Is there a way I can work with my initial temp1 and temp2 nodes to get the desired xml structure?

最满意答案

你的情况有点不理想(我非常支持你给出的工作片段)。

这是什么工作:

pt.add_child("main.elements.element", temp1.get_child("element")); pt.add_child("main.elements.element", temp2.get_child("element"));

住在科利鲁

#include <boost/property_tree/xml_parser.hpp> #include <iostream> using boost::property_tree::ptree; int main() { ptree temp1; temp1.put("element.<xmlattr>.name","elem1"); temp1.put_child("element.temp", {}); ptree temp2; temp2.put("element.<xmlattr>.name","elem2"); temp2.put("element.temp", ""); //Add temp1 and temp2 to <main> under <elements> ptree pt; pt.add_child("main.elements.element", temp1.get_child("element")); pt.add_child("main.elements.element", temp2.get_child("element")); write_xml(std::cout, pt, boost::property_tree::xml_writer_make_settings<std::string>(' ', 4, "utf-8")); }

打印

<?xml version="1.0" encoding="utf-8"?>
<main>
    <elements>
        <element name="elem1">
            <temp/>
        </element>
        <element name="elem2">
            <temp/>
        </element>
    </elements>
</main>

Your situation is a bit suboptimal (and I'd very much favour the working snippet you gave).

Here's what would work:

pt.add_child("main.elements.element", temp1.get_child("element")); pt.add_child("main.elements.element", temp2.get_child("element"));

Live On Coliru

#include <boost/property_tree/xml_parser.hpp> #include <iostream> using boost::property_tree::ptree; int main() { ptree temp1; temp1.put("element.<xmlattr>.name","elem1"); temp1.put_child("element.temp", {}); ptree temp2; temp2.put("element.<xmlattr>.name","elem2"); temp2.put("element.temp", ""); //Add temp1 and temp2 to <main> under <elements> ptree pt; pt.add_child("main.elements.element", temp1.get_child("element")); pt.add_child("main.elements.element", temp2.get_child("element")); write_xml(std::cout, pt, boost::property_tree::xml_writer_make_settings<std::string>(' ', 4, "utf-8")); }

Prints

<?xml version="1.0" encoding="utf-8"?>
<main>
    <elements>
        <element name="elem1">
            <temp/>
        </element>
        <element name="elem2">
            <temp/>
        </element>
    </elements>
</main>

                    
                     
          

更多推荐

本文发布于:2023-08-03 03:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382907.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ptree   子子项   Adding   boost   child

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!