zoukankan      html  css  js  c++  java
  • Boost 解析xml——插入Item

    XML格式为

    <?xml version="1.0" encoding="utf-8"?>
    <Config>  
      <Item name="A" desc="">
            <ChildItem name="name" desc="" datatype="string">11111</ChildItem>
            <ChildItem name="subject" desc="" datatype="string">22222</ChildItem>
            <ChildItem name="desc" desc="" datatype="string">33333</ChildItem>
            <ChildItem name="state" desc="" datatype="int">444444</ChildItem>
            <ChildItem name="ID" desc="" datatype="int">55555</ChildItem>
       </Item>
    </Config>

    需求:将另一段同样格式xml的Item插入到现在这个Item下面

    <?xml version="1.0" encoding="utf-8"?>
    <Config>  
      <Item name="A" desc="">
            <ChildItem name="name" desc="" datatype="string">11111</ChildItem>
            <ChildItem name="subject" desc="" datatype="string">22222</ChildItem>
            <ChildItem name="desc" desc="" datatype="string">33333</ChildItem>
            <ChildItem name="state" desc="" datatype="int">444444</ChildItem>
            <ChildItem name="ID" desc="" datatype="int">55555</ChildItem>
       </Item>
       <Item name="B" desc="">
            <ChildItem name="name" desc="" datatype="string">11111</ChildItem>
            <ChildItem name="subject" desc="" datatype="string">22222</ChildItem>
            <ChildItem name="desc" desc="" datatype="string">33333</ChildItem>
            <ChildItem name="state" desc="" datatype="int">444444</ChildItem>
            <ChildItem name="ID" desc="" datatype="int">55555</ChildItem>
       </Item>
    </Config>


    这样

    代码:

      wstring filePath = m_resourceFilePath.m_TrainingResourcePath + pStrid + m_resourceFilePath.m_TrainingXMLFilePath;
        CPLSimXmlConfigurationFile m_manageConfig;
       // m_manageConfig.m_filename;
        m_manageConfig.InitConfigFile(PLSimLocale::WStringToString(filePath));
        wptree read_pt;
        //原来的xml直接复制过去,因为是第一个循环所以只会复制第一个xml
        wptree copy_pt;
        copy_pt = m_manageConfig.m_pt;
        try
        {
            //判断库文件是否有xml内容
            std::locale utf8Locale(std::locale(), new std::codecvt_utf8<wchar_t>);
            boost::property_tree::read_xml(PLSimLocale::WStringToString(m_resourceFilePath.m_ResourceStoreXMLPath), read_pt, boost::property_tree::xml_parser::trim_whitespace, utf8Locale);
            //没有xml,直接将第一个xml复制过来
            if (!read_pt.get_child_optional(L"Config"))
            {
                copy_pt = copy_pt.get_child(L"Config");
                for (wptree::assoc_iterator iter = copy_pt.find(L"Item"); iter != copy_pt.not_found(); ++iter)
                {
                   wstring strFirstAttrName = iter->second.get<wstring>(L"<xmlattr>.name");
                }
                std::locale utf8Locale(std::locale(), new std::codecvt_utf8<wchar_t>);
                auto settings = boost::property_tree::xml_writer_make_settings<std::wstring>(L'	', 1);
                write_xml(PLSimLocale::WStringToString(m_resourceFilePath.m_ResourceStoreXMLPath), m_manageConfig.m_pt, utf8Locale, settings);
    
    
            }
            else
            {
                //从每个配置文件读第一个Item属性
                wstring strConfigAttrName;
                //判断库文件里xml结构是否完整
                if (read_pt.get_child_optional(L"Config.Item"))
                {
                    wptree copy_pt1 = copy_pt;
                    copy_pt1 = copy_pt1.get_child(L"Config");
                    for (wptree::assoc_iterator iter = copy_pt1.find(L"Item"); iter != copy_pt1.not_found(); ++iter)
                    {
                        strConfigAttrName = iter->second.get<wstring>(L"<xmlattr>.name");
                    }
                    //去掉从库文件里读的头
                    read_pt = read_pt.get_child(L"Config");
                    //去掉从配置文件中读的头
                    copy_pt = copy_pt.get_child(L"Config.Item");
                    //合并xml
                    wptree array_pt;
                    array_pt.add_child(L"Item", read_pt);
                    array_pt = array_pt.get_child(L"Item");
                    auto& b = array_pt.add_child(L"Item", copy_pt);
                    b.put(L"<xmlattr>.name", strConfigAttrName);
                    b.put(L"<xmlattr>.desc", L"");
                    wptree all_pt;
                    all_pt.put_child(L"Config", array_pt);
                    std::locale utf8Locale(std::locale(), new std::codecvt_utf8<wchar_t>);
                    auto settings = boost::property_tree::xml_writer_make_settings<std::wstring>(L'	', 1);
                    write_xml(PLSimLocale::WStringToString(m_resourceFilePath.m_ResourceStoreXMLPath), all_pt, utf8Locale, settings);
                }
            }
        }
        catch (boost::property_tree::ptree_bad_path& e)
        {
            m_error = PLSimLocale::StringToWString(e.what());
            return false;
        }
        catch (boost::property_tree::ptree_bad_data& e)
        {
            m_error = PLSimLocale::StringToWString(e.what());
            return false;
        }
  • 相关阅读:
    Nginx和php是怎么通信的?
    浏览器输入URL到响应页面的全过程
    一个简单清晰的Redis操作类-php
    两种简单的方法Docker构建LANMP
    Docker镜像的构成__Dockerfile
    进入Docker容器
    Docker镜像的构成__docker commit
    Docker的安装
    PHP基于TP5使用Websocket框架之GatewayWorker开发电商平台买家与卖家实时通讯
    OC @property @synthesize和id
  • 原文地址:https://www.cnblogs.com/ye-ming/p/7440450.html
Copyright © 2011-2022 走看看