boost的xml处理(最全攻略)

<?xml version="1.0" encoding="utf-8"?>
<root>
    <root1>
        <root11 A="Tcp" B="" C=""/>
        <root11 A="Tcp" B="" C=""/>
    </root1>
    <root2>
        <root21 B="3106"/>
    </root2>
    <root3>
        <root31>123456789</root31>
        <root32>4444</root32>
        <root33/>
        <root34/>
        root35/>
    </root3>
</server>
一、读增删改(root1的修改)
struct STRCONF
{
    std::wstring A;
    std::wstring B;
    std::wstring C;
};
//1.读取
    std::wstring fileName = L"D://server.xml";
    try
    {
        std::wifstream file(fileName);
        boost::property_tree::wptree pt;
        boost::property_tree::xml_parser::read_xml(file, pt);
        auto root1 = pt.get_child(L"root.root1");
        //读取
        //<root11 A="Tcp" B="" C=""/>
        //<root11 A="Tcp" B="" C=""/>
        for (auto it = root1.begin(); it != root1.end(); ++it)
        {
            auto& node = it->second;
            STRCONF conf;
            conf.name = node.get<std::wstring>(L"<xmlattr>.A", L"");
            conf.ip = node.get<std::wstring>(L"<xmlattr>.B", L"");
            conf.port = node.get<std::wstring>(L"<xmlattr>.C", L"");
        }
        bRet = true;
    }
    catch ( ... )
    {

    }
//2.增加
    try
    {
        QString fileName= QString::fromStdWString( L"D://server.xml");
        boost::property_tree::xml_writer_settings<wstring> settings = 
            boost::property_tree::xml_writer_make_settings<wstring>('\t', 1);
        boost::property_tree::wptree ptUi;  
        boost::property_tree::xml_parser::read_xml(fileName.toStdString(), ptUi, boost::property_tree::xml_parser::trim_whitespace);
        boost::property_tree::wptree pt;    
        pt.add(L"<xmlattr>.A", L"new");
        pt.add(L"<xmlattr>.B", L"new");
        pt.add(L"<xmlattr>.C", L"new");
        ptUi.add_child(L"root.root1.root11", pt);
        //<root1>下增加一条<root11 A="new" B="new" C="new"/>
        boost::property_tree::xml_parser::write_xml(fileName.toStdString(), ptUi, std::locale(),settings);

    }
    catch ( ... )
    {

    }
//3.删除第0个
    try
    {
        boost::property_tree::xml_writer_settings<wstring> settings = 
            boost::property_tree::xml_writer_make_settings<wstring>('\t', 1);   
        std::wstring fileName = L"D://server.xml";
        boost::property_tree::wptree ptSer;
        QString str = QString::fromStdWString(fileName);
        boost::property_tree::xml_parser::read_xml(str.toStdString(), ptSer, boost::property_tree::xml_parser::trim_whitespace);
        auto &root1 = ptSer.get_child(L"root.root1");
        int i = 0;
        //删除这一条<root11 A="Tcp" B="" C=""/>
        for (auto &it = root1.begin(); it != root1.end(); ++it)
        {
            auto& node = it->second;
            all_conf conf;
            if (i == 0)
            {
                root1.erase(it);
                break;
            }
            i++;
        }
        boost::property_tree::xml_parser::write_xml(str.toStdString(), ptSer, std::locale(), settings);
    }
    catch ( ... )
    {

    }

//4.修改
        try
        {
            boost::property_tree::xml_writer_settings<wstring> settings = 
                boost::property_tree::xml_writer_make_settings<wstring>('\t', 1);
            std::wstring fileName =L"D://server.xml";
            boost::property_tree::wptree ptSer;
            QString str = QString::fromStdWString(fileName);
            boost::property_tree::xml_parser::read_xml(str.toStdString(), ptSer, boost::property_tree::xml_parser::trim_whitespace);
            auto &root1 = ptSer.get_child(L"root.root1");
            int i = 0;
            //<root11 A="Tcp" B="" C=""/>修改为<root11 A="11111111111" B="11111111111" C="11111111111"/>
            for (auto &it = root.begin(); it != root.end(); ++it)
            {
                auto& node = it->second;
                if (i == 0)
                {
                    node.put<std::wstring>(L"<xmlattr>.A", L"11111111111");
                    node.put<std::wstring>(L"<xmlattr>.B", L"11111111111");
                    node.put<std::wstring>(L"<xmlattr>.C", L"11111111111");
                    break;
                }
                i++;
            }
            boost::property_tree::xml_parser::write_xml(str.toStdString(), ptSer, std::locale(), settings);
        }
        catch ( ... )
        {

        }

二、读增删改(root3的修改)
1.读取root3
        std::wstring fileName = L"D://server.xml";
        std::wifstream file(fileName);
        boost::property_tree::wptree pt;
        boost::property_tree::xml_parser::read_xml(file, pt);
        auto root3 = pt.get_child(L"root.root3");
        i = 0;
        //读取        
        //<root31>123456789</root31>
        //<root32>4444</root32>
        for (auto it = root3.begin(); it != root3.end(); ++it)
        {   
            auto& node = it->second;
            if (i == 0)
            {
                m_conf.ipDatabase = node.get<std::wstring>(L"", L"");
            }
            else if (i == 1)
            {
                m_conf.portDatabase = node.get<std::wstring>(L"", L"");
            }
            i++;
        }
4.修改
    //修改
        //<root31>123456789</root31>
        //<root32>4444</root32>
        //为
        //<root31>11111</root31>
        //<root32>11111</root32>
    boost::property_tree::xml_writer_settings<wstring> settings = 
        boost::property_tree::xml_writer_make_settings<wstring>('\t', 1);
    QString fileName = QString::fromStdWString(L"D://server.xml");
    try
    {
        boost::property_tree::wptree pt;    
        boost::property_tree::xml_parser::read_xml(fileName.toStdString(), pt, boost::property_tree::xml_parser::trim_whitespace);
        pt.put(L"root.root3.root31", L"11111");
        pt.put(L"root.root3.root32", L"11111");
        boost::property_tree::xml_parser::write_xml(uiFileName.toStdString(), pt, std::locale(),settings);
    }
    catch{}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值