<?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>
</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'\t', 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'\t', );
write_xml(fileName, m_pt, utf8Locale, settings);

Boost解析xml——xml写入的更多相关文章

  1. BOOST 解析,修改,生成xml样例

    解析XML 解析iworld XML,拿到entity和VisibleVolume的数据 int ParseiWorlds::readXML(const bpath &dir) { ptree ...

  2. android XMl 解析神奇xstream 五: 把复杂对象转换成 xml ,并写入SD卡中的xml文件

    前言:对xstream不理解的请看: android XMl 解析神奇xstream 一: 解析android项目中 asset 文件夹 下的 aa.xml 文件 android XMl 解析神奇xs ...

  3. 使用boost/property_tree进行XML操作

    之前一直用tinyxml来进行XML文件操作,刚刚接触的一个测试项目是使用boost操作的,虽然不清楚这两者的差异,但boost使用起来还挺方便的,所以简单整理一些关于boost解析和创建XML文件的 ...

  4. python3.4.3 调用http接口 解析response xml后插入数据库

    工作中需要调用一个http的接口,等不及java组开发,就试着用python去调用.Python版本3.4.3 完整的流程包括:从sqlServer取待调用的合同列表 -> 循环调用http接口 ...

  5. JSON解析和XML解析对比

    JSON解析和XML解析是较为普遍的两种解析方式,其中JSON解析的市场分额更大.本文系统的分析两种解析方式的区别,为更好地处理数据作准备.由于目前阶段主要是做移动开发,所以本文所描述的JSON解析和 ...

  6. 数据解析(XML和JSON数据结构)

    一   解析 二 XML数据结构 三 JSON 数据结构     一 解析 1  定义: 从事先规定好的格式中提取数据     解析的前提:提前约定好格式,数据提供方按照格式提供数据.数据获取方则按照 ...

  7. HL7 2.6解析转XML(C#版)

    HL7 2.6解析转XML(C#版) 项目中需要解析HL7,所以在网上找到解析代码,但错误很多,所以我修改了一下,测试好用. using System; using System.Collection ...

  8. Java解析Soap XML

    package com.jstrd.tipstock.webservice.jt.base; import java.io.ByteArrayInputStream; import java.util ...

  9. Android Animation学习(三) ApiDemos解析:XML动画文件的使用

    Android Animation学习(三) ApiDemos解析:XML动画文件的使用 可以用XML文件来定义Animation. 文件必须有一个唯一的根节点: <set>, <o ...

  10. JSON解析和XML解析

    一. XML:用到一个开源解析类,GDataXMLNode(将其加入项目中),添加libxml2.dylib框架 经常用到的方法: 1.- (id)initWithXMLString:(NSStrin ...

随机推荐

  1. vue9 计算属性 computed

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Windows cannot find ". Make sure you typed the name correctly, and then try again

    https://answers.microsoft.com/en-us/windows/forum/windows_10-desktop/windows-10-explorerexe-error-wi ...

  3. shrio 身份认证流程-Realm

    身份认证流程 流程如下: 1.首先调用Subject.login(token)进行登录,其会自动委托给Security Manager,调用之前必须通过SecurityUtils. setSecuri ...

  4. 基于CANopen DSP402的运动控制笔记

    常用的mode of operation 有以下几种: 控制字 control word: 6--------------7---------------15--------------------7 ...

  5. 织梦(dedecms)彩色标签云(tag)随机颜色和字体大小

    1.选择你所要加页面的模板,一般是在首页index.htm 加上如下代码 01 <!-- /下面开始tag标签云 --> 02 <dl class="tbox light& ...

  6. react-native使用androidstudio时,安卓模拟器reload菜单界面显示快捷键ctrl+M;

    react-native使用androidstudio时,安卓模拟器reload菜单界面显示快捷键ctrl+M:

  7. Linux Mint 19.1将采用新的桌面布局

    我们期待Linux Mint 19.1的发布在圣诞节假期之前到来,希望它会带来一些惊喜. Linux 19.1版本将默认包含Cinnamon 4.0桌面环境,Mint的开发人员说,这将比现在“看起来更 ...

  8. Swift学习笔记(12)--数组和字典的复制

    Swift中,数组Array和字典Dictionary是用结构来实现的,但是数组与字典和其它结构在进行赋值或者作为参数传递给函数的时候有一些不同. 并且数组和字典的这些操作,又与Foundation中 ...

  9. AI:OPENCV实现人脸的自动识别

    依赖jar包: faceRecognition.java package opencv; import java.awt.Graphics; import java.awt.image.Buffere ...

  10. HttpClient方式调用接口的java 简单案例源码+附jar包

    1 package com.itNoob.httpClient; import org.apache.commons.httpclient.HttpClient; import org.apache. ...