• Boost解析xml——xml写入


    1 <?xml version="1.0" encoding="utf-8"?>
    2 <Config>
    3     <Item name="IP测试报告2017-10-24 09-54-31">
    4         <ChildItem name="Date" desc="" datatype ="string">2017-10-24 09-54-31</ChildItem>
    5         <ChildItem name="FailureCount" desc="" datatype ="int">1</ChildItem>
    6         <ChildItem name="IPAddressCount" desc="" datatype ="int">4</ChildItem>
    7         <ChildItem name="SuccessCount" desc="" datatype ="int">3</ChildItem>
    8     </Item>
    9 </Config>

    如何生成以上xml

      for (map<wstring, map<wstring, HistoryData>>::iterator it = vect_str.begin(); it != vect_str.end(); it++)
            { //迭代vector  
                for (map<wstring, HistoryData>::iterator it1 = it->second.begin(); it1 != it->second.end(); it1++)
                {
                    m_pt3.add(L"<xmlattr>.name", it1->second.name);
                    m_pt3.add(L"<xmlattr>.desc", it1->second.desc);
                    m_pt3.add(L"<xmlattr>.datatype ", it1->second.datatype);
                    m_pt3.put_value(it1->second.value);          
                    m_pt2.add_child(L"ChildItem", m_pt3);  
                    m_pt3.clear();
                }
                
                m_pt1.add_child(L"Item", m_pt2);
                m_pt1.add(L"Item.<xmlattr>.name", it->first);
                m_pt2.clear();
    } m_pt.add_child(L"Config", m_pt1);
        auto settings = boost::property_tree::xml_writer_make_settings<std::wstring>(L' ', 1);
        write_xml(fileName, m_pt, utf8Locale, settings);


    如何在现有的xml里插入新的Item如下

    <?xml version="1.0" encoding="utf-8"?>
    <Config>
        <Item name="IP测试报告2017-10-24 09-54-31">
            <ChildItem name="Date" desc="" datatype="string">2017-10-24 09-54-31</ChildItem>
            <ChildItem name="FailureCount" desc="" datatype="int">1</ChildItem>
            <ChildItem name="IPAddressCount" desc="" datatype="int">4</ChildItem>
            <ChildItem name="SuccessCount" desc="" datatype="int">3</ChildItem>
        </Item>
        <Item name="IP测试报告2017-10-24 09-54-32">
            <ChildItem name="Date" desc="" datatype ="string">2017-10-24 09-54-32</ChildItem>
            <ChildItem name="FailureCount" desc="" datatype ="int">1</ChildItem>
            <ChildItem name="IPAddressCount" desc="" datatype ="int">4</ChildItem>
            <ChildItem name="SuccessCount" desc="" datatype ="int">3</ChildItem>
        </Item>
    </Config>

    代码

     if (auto firstChild = m_pt.get_child_optional(L"Config"))
        {
            m_pt4 = m_pt.get_child(L"Config");
            m_pt.clear();
            for (map<wstring, map<wstring, HistoryData>>::iterator it = vect_str.begin(); it != vect_str.end(); it++)
            { //迭代vector  
                for (map<wstring, HistoryData>::iterator it1 = it->second.begin(); it1 != it->second.end(); it1++)
                {
                    m_pt3.add(L"<xmlattr>.name", it1->second.name);
                    m_pt3.add(L"<xmlattr>.desc", it1->second.desc);
                    m_pt3.add(L"<xmlattr>.datatype ", it1->second.datatype);
                    m_pt3.put_value(it1->second.value);
                    m_pt2.add_child(L"ChildItem", m_pt3);
                    m_pt3.clear();
                }
                m_pt2.add(L"<xmlattr>.name", it->first);
                m_pt4.add_child(L"Item", m_pt2);
                m_pt2.clear();
            } 
            m_pt.add_child(L"Config", m_pt4);
        }
        else
        {
            for (map<wstring, map<wstring, HistoryData>>::iterator it = vect_str.begin(); it != vect_str.end(); it++)
            { //迭代vector  
                for (map<wstring, HistoryData>::iterator it1 = it->second.begin(); it1 != it->second.end(); it1++)
                {
                    m_pt3.add(L"<xmlattr>.name", it1->second.name);
                    m_pt3.add(L"<xmlattr>.desc", it1->second.desc);
                    m_pt3.add(L"<xmlattr>.datatype ", it1->second.datatype);
                    m_pt3.put_value(it1->second.value);         
                    m_pt2.add_child(L"ChildItem", m_pt3);
                    m_pt3.clear();
                }
                m_pt1.add_child(L"Item", m_pt2);
                m_pt1.add(L"Item.<xmlattr>.name", it->first);
                m_pt2.clear();
            }
            m_pt.add_child(L"Config", m_pt1);      
        }
        auto settings = boost::property_tree::xml_writer_make_settings<std::wstring>(L'	', 1);
        write_xml(fileName, m_pt, utf8Locale, settings);
  • 相关阅读:
    [OpenStack] openstack keystone api 实验(curl)(转载)(亲自动手实验,附带实验运行结果)
    [Hadoop in China 2011] 中兴:NoSQL应用现状及电信业务实践
    [转]Windows Server 2012 和 System Center 2012 SP1,Virtual Machine Manager 中启用的软件定义的网络
    [Hadoop in China 2011] Facebook Message在HBase基础上的应用
    [转] ETL工具介绍
    [Hadoop in China 2011] eBay:选择HBase建立搜索引擎的原因
    [Cloudera Hadoop] CDH 4.0 Quick Start Guide (动手实践,最新版CDH4.0,企业版Hadoop)
    云操作系统OpenStack 优势与问题并存
    [OpenStack Crowbar] Build Crowbar.ISO
    [OpenStack] OpenStack ESSEX 全新手动安装,动手,实践,出真知!
  • 原文地址:https://www.cnblogs.com/ye-ming/p/7728420.html
Copyright © 2020-2023  润新知