boost的xml处理(最全攻略)

version="1.0" encoding="utf-8"?>

    
        "Tcp" B="" C=""/>
        "Tcp" B="" C=""/>
    
    
        "3106"/>
    
    
        123456789
        4444
        
        
        root35/>
    

一、读增删改(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");
        //读取
        //
        //
        for (auto it = root1.begin(); it != root1.end(); ++it)
        {
            auto& node = it->second;
            STRCONF conf;
            conf.name = node.getwstring>(L".A", L"");
            conf.ip = node.getwstring>(L".B", L"");
            conf.port = node.getwstring>(L".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".A", L"new");
        pt.add(L".B", L"new");
        pt.add(L".C", L"new");
        ptUi.add_child(L"root.root1.root11", pt);
        //下增加一条
        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;
        //删除这一条
        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;
            //修改为
            for (auto &it = root.begin(); it != root.end(); ++it)
            {
                auto& node = it->second;
                if (i == 0)
                {
                    node.putwstring>(L".A", L"11111111111");
                    node.putwstring>(L".B", L"11111111111");
                    node.putwstring>(L".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;
        //读取        
        //123456789
        //4444
        for (auto it = root3.begin(); it != root3.end(); ++it)
        {   
            auto& node = it->second;
            if (i == 0)
            {
                m_conf.ipDatabase = node.getwstring>(L"", L"");
            }
            else if (i == 1)
            {
                m_conf.portDatabase = node.getwstring>(L"", L"");
            }
            i++;
        }
4.修改
    //修改
        //123456789
        //4444
        //为
        //11111
        //11111
    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{}

你可能感兴趣的:(boost的xml处理(最全攻略))