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 ...
随机推荐
- Linux下查看txt文档
当我们在使用Window操作系统的时候,可能使用最多的文本格式就是txt了,可是当我们将Window平台下的txt文本文档复制到Linux平台下查看时,发现原来的中文所有变成了乱码. 没错, 引起这个 ...
- Object和其他类型的转换
Object对象是一切类的父类(基类),只要是Object对象,可以强制转换为其他类型.
- Ubunut PPA源概述
Ubuntu 自带的“软件”应用,可以安装海量软件,既包括发行者支持的软件.社区支持的软件,也包括专有驱动和版权软件.有时,我们需要的软件通过这些渠道仍然无法找到.这时,可以到 PPA 软件源中查找. ...
- 2008R2域控环境中 应用组策略 实现禁用USB设备使用
本文介绍如何在Windows Server 2008 AD中禁用客户端USB端口.本文使用的系统:Windows Server 2008 R2 企业版.域功能级别:Windows Server 200 ...
- window.location.href和window.top.location.href的区别
if (window.location.href == window.top.location.href) { window.top.location.href = "/index. ...
- js垃圾回收机制理解
原理 找到不再被使用的变量,然后释放其占用的内存,但这个过程不是时时的,因为其开销比较大, 所以垃圾回收器会按照固定时间间隔周期性的执行 回收方式 a.标记清除 当变量进入环境时,将这个变量标记为“进 ...
- Reference Counting GC (Part two :Partial Mark & Sweep)
目录 部分标记清除算法 前提 dec_ref_cnt()函数 new_obj()函数 scan_hatch_queue()函数 paint_gray()函数 scan_gray()函数 collect ...
- JAVA 获取访问者IP
* 获取访问者IP * * 在一般情况下使用Request.getRemoteAddr()即可,但是经过nginx等反向代理软件后,这个方法会失效. * * 本方法先从Header中获取X-Real- ...
- 【Codeforces Round #459 (Div. 2) C】The Monster
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 左括号看成1 右括号看成-1 设置l,r表示前i个数的和的上下界 遇到 左括号 l和r同时加1 遇到右括号 同时减1 遇到问号 因为 ...
- es65 跨模块常量
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...