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'\t', );
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'\t', );
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;
}
Boost 解析xml——插入Item的更多相关文章
- boost解析XML方法教程
boost库在解析XML时具有良好的性能,可操作性也很强下地址有个简单的说明 http://blog.csdn.net/luopeiyuan1990/article/details/9445691 一 ...
- Boost解析xml——xml写入
<?xml version="1.0" encoding="utf-8"?> <Config> <Item name=" ...
- JQuery解析XML数据的几个例子
用JavaScript解析XML数据是常见的编程任务,JavaScript能做的,JQuery当然也能做.下面我们来总结几个使用JQuery解析XML的例子. 第一种方案: <script ty ...
- 几个JQuery解析XML的程序例子
用JavaScript解析XML数据是常见的编程任务,JavaScript能做的,JQuery当然也能做.下面我们来总结几个使用JQuery解析XML的例子. 第一种方案: <script ty ...
- boost.property_tree解析xml的帮助类以及中文解析问题的解决(转)
boost.property_tree可以用来解析xml和json文件,我主要用它来解析xml文件,它内部封装了号称最快的xml解析器rapid_xml,其解析效率还是很好的.但是在使用过程中却发现各 ...
- (原创)boost.property_tree解析xml的帮助类以及中文解析问题的解决
boost.property_tree可以用来解析xml和json文件,我主要用它来解析xml文件,它内部封装了号称最快的xml解析器rapid_xml,其解析效率还是很好的.但是在使用过程中却发现各 ...
- boost::property_tree读取解析.xml文件
boost::property_tree读取解析.xml文件 1)read_xml 支持中文路径 boost::property_tree::wptree wpt; std::locale:: ...
- Ajax实现xml文件数据插入数据库(一)--- 构建解析xml文件的js库
Ajax实现将xml文件数据插入数据库的过程所涉及到的内容比较多,所以对于该过程的讲解本人打算根据交互的过程将其分为三个部分,第一部分为构建解析xml文件的javascript库,第二部分为ajax与 ...
- BOOST 解析,修改,生成xml样例
解析XML 解析iworld XML,拿到entity和VisibleVolume的数据 int ParseiWorlds::readXML(const bpath &dir) { ptree ...
随机推荐
- 杭电1425 sort
Problem Description 给你n个整数.请按从大到小的顺序输出当中前m大的数. Input 每组測试数据有两行,第一行有两个数n,m(0<n,m<1000000).第二行 ...
- Docker Network Configuration 高级网络配置
Network Configuration TL;DR When Docker starts, it creates a virtual interface named docker0 on the ...
- 动态代理(jdk--cglib)
JAVA的动态代理 代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等.代理类与委托类 ...
- 深入Vue的响应式原理
工作的过程中,有时候会有数据改变但是视图没有更新的问题,作者在vue的官方文档中有提到这个问题,我来总结一下 1.vue的每个组件实例都有对象的watcher实例对象,它会在组件渲染的过程中把属性记录 ...
- centos 7.3 配置vnc 服务 图形界面登录
1.检查系统是否有安装tigervnc-server软件包 rpm -qa |grep vnc 默认的系统未装tigervnc-server软件包 2.安装tigervnc-server软件包 yum ...
- 分享js中的 sort 另一种用法
// 看上去正常的结果: ['Google', 'Apple', 'Microsoft'].sort(); // ['Apple', 'Google', 'Microsoft']; // apple排 ...
- 基于SIFT的点云关键点提取
这篇博文主要介绍SIFT算法在提取点云图像关键点时的具体用法. 尺度不变特征转换(Scale-invariant feature transform,SIFT)是David Lowe在1999年发表, ...
- AtCoder Beginner Contest 067 D - Fennec VS. Snuke
D - Fennec VS. Snuke Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Statement F ...
- nodejs操作文件和数据流
前言 node中有一组流api,它们可以像处理网络流一样处理文件.流api用起来非常方便,本节学习介绍文件处理基础和流的概念. 目录 处理文件路径 fs核心模块简介 操作流 慢客户端问题 1. 处理文 ...
- Windows上Python2与Python3同时安装、共存
一.选择 Python2 还是 Python3?当然是全都要 Python3 虽是未来,不过 Python2 的用户群体仍然膨大,网上有大量优良的项目和模块可供使用,遇到问题也基本可以找到解决方法,推 ...