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 ...
随机推荐
- RedHat Linux 多媒体学习指南 (共 36 部原创视频)
1.为sco unix 添加第二块网卡 [url]http://you.video.sina.com.cn/b/11695632-1443650204.html[/url] 2.为sco unix ...
- SQL try catch处理
ALTER PROC usp_AccountTransaction @AccountNum INT, @Amount DECIMAL AS BEGIN BEGIN TRY --Start the Tr ...
- Svn备份与Bandizip压缩批处理程序
目的:为了定时备份多个svn仓库数据,使用批处理程序进行备份并Bandizip进行压缩保存到指定位置,操作完成后弹出成功提示. 为了完成以上目标,需要了解以下几个方面: 批处理命令 Svn命令 Ban ...
- Idea下mybatis的错误—Module not specified
IDEA下使用maven的mybatis常见错误 错误类型一:导入项目引起的错误Module not specified 错误提示:idea Error Module not specified. 错 ...
- Windows上Python2与Python3同时安装、共存
一.选择 Python2 还是 Python3?当然是全都要 Python3 虽是未来,不过 Python2 的用户群体仍然膨大,网上有大量优良的项目和模块可供使用,遇到问题也基本可以找到解决方法,推 ...
- div设置了居中和宽度,但是显示时宽度占100%
<div id="bigDiv" align="center"> <div id="bottom" style=" ...
- PIM-DM协议内核触发机制及协议执行机制记录
PIM-DM和PIM-SM是组播路由ASM(随意信源组播)中的两种不同模式.相对PIM-SM的组播注冊机制.PIM-DM的扩散机制显得更加粗犷. 一.PIM-DM无需向内核注冊pimreg虚接口. 可 ...
- servlet学习(1)
1.Servlet是sun公司提供的一门用于开发动态web资源的技术. 2.Servlet在web应用的位置: 3.创建Servlet的三种方式: (1)实现servlet的接口 (2)继承Gener ...
- cmake 常见问题及解决
1. undefined reference to symbol 'pthread_key_delete@@GLIBC_2.2.5 未定义对某符号的引用,该错误为链接时(linking)发生的错误.有 ...
- 3.Maven之(三)Maven插件
转自:https://yq.aliyun.com/ziliao/312162 Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,像编译是通过maven- ...