How to copy subtree from Boost.PropertyTree

耗尽温柔 提交于 2019-12-20 07:17:45

问题


I have some boost::property_tree::ptree. I need tree with removing some elements with certain tag name. For example, xml for source ptree is the following:

<?xml version="1.0" encoding="utf-8"?>
<document>
  <B atr="one" atr1="something">
    <to_remove attr="two">10</to_remove>
  </B>
  <to_remove>
    <C>value</C>
    <D>other value</D>
  </to_remove>
  <E>nothing</E>
</document>

And I'd like to get ptree with xml like the following:

<?xml version="1.0" encoding="utf-8"?>
<document>
  <B atr="one" atr1="something" />
  <E>nothing</E>
</document>

How to write function, that generate new ptree with removed <to_remove> nodes?


回答1:


The value_type of ptree is std::pair< const Key, self_type >, so you can iterate the tree and remove corresponding nodes. The following is a sample.

void remove(ptree &pt){
using namespace boost::property_tree;
    for (auto p = pt.begin(); p != pt.end();){
        if (p->first == "to_remove"){
            p = pt.erase(p);
        }
        else{
            remove(p->second);
            ++p;
        }
    }
}



回答2:


UPDATE Replaced my answer due to the comment: I'd recommend, instead, to use a proper XML library.

I believe Boost PropertyTree uses a modified RapidXml internally (but it's an implementation detail, so I'm not sure I would depend on it). Here's my take on it using PugiXML, which is a modern, header-only, non-validating XML library:

#include <pugixml.hpp>
#include <iostream>

int main()
{
    pugi::xml_document doc;
    doc.load_file("input.txt");

    for (auto& to_remove : doc.select_nodes("descendant-or-self::to_remove/.."))
        while (to_remove.node().remove_child("to_remove"));

    doc.save(std::cout);
}

Prints

<?xml version="1.0"?>
<document>
    <B atr="one" atr1="something" />
    <E>nothing</E>
</document>


来源:https://stackoverflow.com/questions/24235589/how-to-copy-subtree-from-boost-propertytree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!