zoukankan      html  css  js  c++  java
  • boost write_xml不保存attribute 备忘

    Attributes in property tree are added as sub-nodes. 

    property tree 属性作为子节点 (坑坑坑坑坑)

    使用

    write_xml_element(std::cout, pt.front().first, pt.front().second, 0, boost::property_tree::xml_writer_make_settings<std::string>(' ', 0));

    1 You can use the undocumented function write_xml_element to write the single element (using the key as element name):
    
        // write the single element: (undocumented API)
        boost::property_tree::xml_parser::write_xml_element(
                std::cout, it->first, it->second,
                0, settings
            );
    2 or you can create a new ptree object with the single child
    
        ptree tmp;
        tmp.add_child(it->first, it->second);
        write_xml(std::cout, tmp, settings);
    #include <iostream>
    #include <boost/property_tree/xml_parser.hpp>
    #include <boost/property_tree/ptree.hpp>
    using namespace std;
    
    int main() {
       
    auto str=R"(xmlcontent)";
    using namespace boost::property_tree;
           iptree tree;
        stringstream ss;
        ss<<str;
        boost::property_tree::xml_parser::read_xml(ss,tree);
        
        
            iptree pchilds = tree.get_child("MainTask");
           // write_xml(std::cout, tree);
            for (auto pos= pchilds.begin(); pos != pchilds.end(); ++pos)
            {
                bool decode_flag = false;
                std::string name = pos->first;
                iptree tempNode = pos->second;
                if (name == "<xmlattr>") {
                    
                    continue;
                }
                
                if (name == "subnode") //子节点
                {
    //获取所有子节点

    iptree temppp;
    auto& attributes = tempNode.get_child("<xmlattr>", temppp);
    for(auto &v: attributes){
    cout << "First: " << v.first.data() << " Second: " << v.second.data() << endl;
    }


    for(auto child:tempNode.get_child("")){ //if( child.first == "<xmlattr>" ) { // write_xml(std::cout,child.second); // write_xml_element(std::cout, child.first, child.second, 0, boost::property_tree::xml_writer_make_settings<std::string>(' ', 0)); write_xml_element(std::cout,ptree::key_type(),child.second,0,xml_writer_make_settings<std::string>('', 0)); //cout<<child.second.data()<<endl; // } } //string sss=tempNode.get("<xmlattr>", ""); //std::cout<<tempNode.data()<<endl; //write_xml(std::cout,tempNode ,options); } } }

    参考:

    https://www.titanwolf.org/Network/q/d9d0c6a1-6771-4da9-985e-a7975028ebee/y

    https://stackoverflow.com/questions/14010473/parsing-xml-attributes-with-boost

    https://www.technical-recipes.com/2014/using-boostproperty_tree/

    使用

    http://www.ce.unipr.it/people/medici/boost_ptree.html

    https://topic.alibabacloud.com/a/boost-ptree-operation-xml-easy-and-useful_8_8_10266710.html

    关键点

    https://stackoverflow.com/questions/53603392/boost-property-tree-iterating-through-attributes-of-repeated-child-elements-wit

    https://stackoverflow.com/questions/16058204/omit-xml-declaration-when-saving-xml-with-boost

    忽略声明

    https://stackoverflow.com/questions/47448864/removing-encoding-attribute-from-xml-using-boost

    https://stackoverflow.com/questions/16058204/omit-xml-declaration-when-saving-xml-with-boost

    格式化

    https://stackoverflow.com/questions/55980786/how-to-serialize-a-subtree-of-a-boost-property-tree-to-xml

    https://stackoverflow.com/questions/35954432/how-to-get-all-child-node-values

  • 相关阅读:
    hdu 1269 迷宫城堡 (并查集)
    hdu 1272 小希的迷宫 (深搜)
    hdu 1026 Ignatius and the Princess I (深搜)
    hdu 1099 Lottery
    hdu 1068 Girls and Boys (二分匹配)
    几个基础数位DP(hdu 2089,hdu 3555,uestc 1307 windy 数)
    hdu 1072 Nightmare (广搜)
    hdu 1398 Square Coins (母函数)
    hdu 1253 胜利大逃亡 (深搜)
    hdu 1115 Lifting the Stone (求重心)
  • 原文地址:https://www.cnblogs.com/wolbo/p/14659963.html
Copyright © 2011-2022 走看看