欢迎访问Ningto's博客

Menu
  • 首页
  • 归档
  • 关于
  • 书签
  • 必应壁纸
  • IT聚合
  • 工具
    • 我的工具列表
    • 我的网盘
    • 必应每日壁纸API
    • Html转Markdown
    • 仙尘光标
Menu

boost xml

最后更新 2017-10-24 14:02:42   阅读量 2078

读写XML文件: testConfigRead.xml

<?xml version="1.0" encoding="GB2312"?>
<content>
  <title value="xxxx"/>
  <number>1234</number>
  <groups>
    <class num="1" type="type1"/>
    <class num="2" type="type2"/>
    <class num="3" type="type3"/>
  </groups>
  <classes>
    <name>first</name>
    <name>second</name>
    <name>third</name>
  </classes>
</content>

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>
#include <boost/typeof/std/utility.hpp>
#include <string>
#include <map>
#include <set>
#include <exception>
#include <iostream>

struct TestConfig
{
    std::string title;
    int number;
    std::map<int, std::string> groups;
    std::set<std::string> classes;
    void load(const std::string& filename);
    void save(const std::string& filename);
};

void TestConfig::load(const std::string& filename)
{
    using boost::property_tree::ptree;
    ptree pt;
    read_xml(filename, pt, boost::property_tree::xml_parser::trim_whitespace);

    title = pt.get_child("content.title").get<std::string>("<xmlattr>.value");
    std::cout << title << std::endl;

    number = pt.get<int>("content.number");
    std::cout << number << std::endl;

    ptree &groups_node = pt.get_child("content.groups");
    BOOST_FOREACH(const ptree::value_type& vt, groups_node)
    {
        std::string num = vt.second.get<std::string>("<xmlattr>.num");
        std::string type = vt.second.get<std::string>("<xmlattr>.type");
        groups.insert(std::pair<int, std::string>(atoi(num.c_str()), type));
        std::cout << num << "," << type << std::endl;
    }

    ptree &classes_node = pt.get_child("content.classes");
    BOOST_FOREACH(const ptree::value_type& vt, classes_node)
    {
        classes.insert(vt.second.data());
        std::cout << vt.second.data() << std::endl;
    }
}

void TestConfig::save(const std::string& filename)
{
    using boost::property_tree::ptree;
    ptree pt, pattr1;

    pattr1.add<std::string>("<xmlattr>.value", title);
    pt.add_child("content.title", pattr1);
    pt.put("content.number", number);

    typedef std::map<int, std::string> map_type;
    BOOST_FOREACH(const map_type::value_type &grp, groups)
    {
        ptree pattr2;
        pattr2.add<int>("<xmlattr>.num", grp.first);
        pattr2.add<std::string>("<xmlattr>.type", grp.second);
        pt.add_child("content.groups.class", pattr2);
    }
    
    BOOST_FOREACH(const std::string& cls, classes)
    {
        pt.add("content.classes.name", cls);
    }
    
    // 格式化输出,指定编码(默认utf-8)
    boost::property_tree::xml_writer_settings<char> settings('\t', 1, "GB2312");
    write_xml(filename, pt, std::locale(), settings);
}


int main()
{
    try
    {
        TestConfig tc;
        tc.load("testConfigRead.xml");
        tc.save("testConfigWrite.xml");
        std::cout << "Success\n";
    }
    catch (std::exception &e)
    {
        std::cout << "Error: " << e.what() << "\n";
    }

    system("pause");
    return 0;
}
(转载本站文章请注明作者和出处:泞途 - ningto.com)

下一篇 – 表达式求值——栈
上一篇 – python模拟鼠标、捕获按键事件

  1. C/C++
  2. Boost

toningto@outlook.com

标签云

IOS Qt Life Linux Java MongoDB Boost MQ Node.js Mac C/C++ Tools Design Python Bug Others Javascript Tips React Android Web Mobile Shell Product Go Database Windows

推广链接

【腾讯云】云产品限时秒杀,爆款1核2G云服务器,首年99元

多谢支持,用了好几年,服务很稳定支持多设备!

其他

文章RSS

Copyright © 2016 Welcome To Ningto Blog | 鄂ICP备17003086号-2