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的更多相关文章

  1. boost解析XML方法教程

    boost库在解析XML时具有良好的性能,可操作性也很强下地址有个简单的说明 http://blog.csdn.net/luopeiyuan1990/article/details/9445691 一 ...

  2. Boost解析xml——xml写入

    <?xml version="1.0" encoding="utf-8"?> <Config> <Item name=" ...

  3. JQuery解析XML数据的几个例子

    用JavaScript解析XML数据是常见的编程任务,JavaScript能做的,JQuery当然也能做.下面我们来总结几个使用JQuery解析XML的例子. 第一种方案: <script ty ...

  4. 几个JQuery解析XML的程序例子

    用JavaScript解析XML数据是常见的编程任务,JavaScript能做的,JQuery当然也能做.下面我们来总结几个使用JQuery解析XML的例子. 第一种方案: <script ty ...

  5. boost.property_tree解析xml的帮助类以及中文解析问题的解决(转)

    boost.property_tree可以用来解析xml和json文件,我主要用它来解析xml文件,它内部封装了号称最快的xml解析器rapid_xml,其解析效率还是很好的.但是在使用过程中却发现各 ...

  6. (原创)boost.property_tree解析xml的帮助类以及中文解析问题的解决

    boost.property_tree可以用来解析xml和json文件,我主要用它来解析xml文件,它内部封装了号称最快的xml解析器rapid_xml,其解析效率还是很好的.但是在使用过程中却发现各 ...

  7. boost::property_tree读取解析.xml文件

    boost::property_tree读取解析.xml文件 1)read_xml 支持中文路径  boost::property_tree::wptree wpt;    std::locale:: ...

  8. Ajax实现xml文件数据插入数据库(一)--- 构建解析xml文件的js库

    Ajax实现将xml文件数据插入数据库的过程所涉及到的内容比较多,所以对于该过程的讲解本人打算根据交互的过程将其分为三个部分,第一部分为构建解析xml文件的javascript库,第二部分为ajax与 ...

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

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

随机推荐

  1. .Net商品管理(注释,百度,提问,对比,总结)

    管理控制器 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sys ...

  2. Pocket英语语法---一、形容词性物主代词和名词性物主代词

    Pocket英语语法---一.形容词性物主代词和名词性物主代词 一.总结 一句话总结:名词性物主代词=形容词性物主代词+名词 Is that your umbrella? This is your c ...

  3. 解决电信或网通的DNS劫持

    大家有没有碰到访问一些不存在域名或者网站时,浏览器本应显示一个网址不存在之类的信息,但是因为现在很多ISP做了DNS劫持将不存在的域名或网址重定向到ISP的广告页面,烦人的狠.其实tomato可以解决 ...

  4. Spring 热点面试题:

    1.谈谈你对Springaop的理解? spring用代理类包裹切面,把他们织入到Spring管理的bean中.也就是说代理类伪装成目标类,它会截取对目标类中方法的调用,让调用者对目标类的调用都先变成 ...

  5. Swift学习笔记(15)--下标脚本(Subscripts)

    下标脚本可以定义在类(Class).结构体(structure)和枚举(enumeration)这些目标中,使用中类似数组或者字典的用法 1.定义 定义下标脚本使用subscript关键字,语法: s ...

  6. 【Codeforces Round #424 (Div. 2) A】Unimodal Array

    [Link]:http://codeforces.com/contest/831/problem/A [Description] 让你判断一个数列是不是这样一个数列: 一开始是严格上升 然后开始全都是 ...

  7. CODEVS——T 1404 字符串匹配

    http://codevs.cn/problem/1404/ 时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解  查看运行结果     题目描述 Desc ...

  8. CF(438D) The Child and Sequence(线段树)

    题意:对数列有三种操作: Print operation l, r. Picks should write down the value of . Modulo operation l, r, x. ...

  9. hdu 5073 Galaxy(2014 鞍山现场赛)

    Galaxy                                                                   Time Limit: 2000/1000 MS (J ...

  10. GO语言为结构体排序

    package main import ( "fmt" "io/ioutil" "sort" "time" ) type ...