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 ...
随机推荐
- EBS TimeZone问题
记录问题 : http://blog.csdn.net/cymm_liu/article/details/29234919 -- Oracle 数据库中的 时间 时区 http:// ...
- JNI学习积累之三 ---- 操作JNI函数以及复杂对象传递
本文原创,转载请注明出处:http://blog.csdn.NET/qinjuning 在掌握了JNI函数的使用和相关类型的映射后,以及知晓何利用javah工具生成对应的jni函数以及如何生成动态 链 ...
- 怎么在cmd中输入mysql就可以进去mysql控制台
执行命令 设置cmd以管理员身份运行
- <QT障碍之路>QApplication:No such file or directory
原因:QT5将很多部件都移动了QT widgets模块中. 解决方法: 在.pro文件中添加 greaterThan(QT_MAJOR_VERSION, ): QT += widgets
- 学习TF:《TensorFlow实战》中文版PDF+源代码
深度学习乃至人工智能正逐渐在FinTech领域发挥巨大的作用,其应用包括自动报告生成.金融智能搜索.量化交易和智能投顾.而TensorFlow为金融业方便地使用深度学习提供了可能.<Tensor ...
- 学习《人工智能一种现代的方法(第3版)》中文PDF+英文PDF
学习人工智能概论时,推荐看看<人工智能:一种现代的方法(第3版)>,最权威.最经典的人工智能教材,已被全世界100多个国家的1200多所大学用作教材. 全面性以及结构的安排还是不错的,值得 ...
- Add Webhooks to Your API the Right Way
Add Webhooks to Your API the Right Way Adam DuVander / December 15, 2016 In the last 10 years, APIs ...
- Cocos2dx 小技巧(十二) 一种可行的系列动画播放方式
今早发生了一件事让我感觉特气愤!去年的这个时候,我和小伙伴们一起在操场上拍毕业照,之后有个当地报纸的记者来我们学校取材,看到我们后打算给我们拍几张创意张扬点的毕业照.之后呢,照片出来了,拍的效果大伙都 ...
- 在oracle存储过程中创建暂时表
在oracle的存储过程中,不能直接使用DDL语句,比方create.alter.drop.truncate等. 那假设我们想在存储过程中建立一张暂时表就仅仅能使用动态sql语句了: create o ...
- eclipse的项目栏
点击window然后选择show view 然后选择project Explorer就会出现项目栏