boost的xml处理(最全攻略)
2017-12-13 19:03
211 查看
<?xml version="1.0" encoding="utf-8"?> <root> <root1> <root11 A="Tcp" B="" C=""/> <root11 A="Tcp" B="" C=""/> </root1> <root2> <root21 B="3106"/> </root2> <root3> <root31>123456789</root31> <root32>4444</root32> <root33/> <root34/> root35/> </root3> </server> 一、读增删改(root1的修改) struct STRCONF { std::wstring A; std::wstring B; std::wstring C; }; //1.读取 std::wstring fileName = L"D://server.xml"; try { std::wifstream file(fileName); boost::property_tree::wptree pt; boost::property_tree::xml_parser::read_xml(file, pt); auto root1 = pt.get_child(L"root.root1"); //读取 //<root11 A="Tcp" B="" C=""/> //<root11 A="Tcp" B="" C=""/> for (auto it = root1.begin(); it != root1.end(); ++it) { auto& node = it->second; STRCONF conf; conf.name = node.get<std::wstring>(L"<xmlattr>.A", L""); conf.ip = node.get<std::wstring>(L"<xmlattr>.B", L""); conf.port = node.get<std::wstring>(L"<xmlattr>.C", L""); } bRet = true; } catch ( ... ) { } //2.增加 try { QString fileName= QString::fromStdWString( L"D://server.xml"); boost::property_tree::xml_writer_settings<wstring> settings = boost::property_tree::xml_writer_make_settings<wstring>('\t', 1); boost::property_tree::wptree ptUi; boost::property_tree::xml_parser::read_xml(fileName.toStdString(), ptUi, boost::property_tree::xml_parser::trim_whitespace); boost::property_tree::wptree pt; pt.add(L"<xmlattr>.A", L"new"); pt.add(L"<xmlattr>.B", L"new"); pt.add(L"<xmlattr>.C", L"new"); ptUi.add_child(L"root.root1.root11", pt); //<root1>下增加一条<root11 A="new" B="new" C="new"/> boost::property_tree::xml_parser::write_xml(fileName.toStdString(), ptUi, std::locale(),settings); } catch ( ... ) { } //3.删除第0个 try { boost::property_tree::xml_writer_settings<wstring> settings = boost::property_tree::xml_writer_make_settings<wstring>('\t', 1); std::wstring fileName = L"D://server.xml"; boost::property_tree::wptree ptSer; QString str = QString::fromStdWString(fileName); boost::property_tree::xml_parser::read_xml(str.toStdStrin 4000 g(), ptSer, boost::property_tree::xml_parser::trim_whitespace); auto &root1 = ptSer.get_child(L"root.root1"); int i = 0; //删除这一条<root11 A="Tcp" B="" C=""/> for (auto &it = root1.begin(); it != root1.end(); ++it) { auto& node = it->second; all_conf conf; if (i == 0) { root1.erase(it); break; } i++; } boost::property_tree::xml_parser::write_xml(str.toStdString(), ptSer, std::locale(), settings); } catch ( ... ) { } //4.修改 try { boost::property_tree::xml_writer_settings<wstring> settings = boost::property_tree::xml_writer_make_settings<wstring>('\t', 1); std::wstring fileName =L"D://server.xml"; boost::property_tree::wptree ptSer; QString str = QString::fromStdWString(fileName); boost::property_tree::xml_parser::read_xml(str.toStdString(), ptSer, boost::property_tree::xml_parser::trim_whitespace); auto &root1 = ptSer.get_child(L"root.root1"); int i = 0; //<root11 A="Tcp" B="" C=""/>修改为<root11 A="11111111111" B="11111111111" C="11111111111"/> for (auto &it = root.begin(); it != root.end(); ++it) { auto& node = it->second; if (i == 0) { node.put<std::wstring>(L"<xmlattr>.A", L"11111111111"); node.put<std::wstring>(L"<xmlattr>.B", L"11111111111"); node.put<std::wstring>(L"<xmlattr>.C", L"11111111111"); break; } i++; } boost::property_tree::xml_parser::write_xml(str.toStdString(), ptSer, std::locale(), settings); } catch ( ... ) { } 二、读增删改(root3的修改) 1.读取root3 std::wstring fileName = L"D://server.xml"; std::wifstream file(fileName); boost::property_tree::wptree pt; boost::property_tree::xml_parser::read_xml(file, pt); auto root3 = pt.get_child(L"root.root3"); i = 0; //读取 //<root31>123456789</root31> //<root32>4444</root32> for (auto it = root3.begin(); it != root3.end(); ++it) { auto& node = it->second; if (i == 0) { m_conf.ipDatabase = node.get<std::wstring>(L"", L""); } else if (i == 1) { m_conf.portDatabase = node.get<std::wstring>(L"", L""); } i++; } 4.修改 //修改 //<root31>123456789</root31> //<root32>4444</root32> //为 //<root31>11111</root31> //<root32>11111</root32> boost::property_tree::xml_writer_settings<wstring> settings = boost::property_tree::xml_writer_make_settings<wstring>('\t', 1); QString fileName = QString::fromStdWString(L"D://server.xml"); try { boost::property_tree::wptree pt; boost::property_tree::xml_parser::read_xml(fileName.toStdString(), pt, boost::property_tree::xml_parser::trim_whitespace); pt.put(L"root.root3.root31", L"11111"); pt.put(L"root.root3.root32", L"11111"); boost::property_tree::xml_parser::write_xml(uiFileName.toStdString(), pt, std::locale(),settings); } catch{}
相关文章推荐
- 使用Boost.PropertyTree处理XML、JSON和INI数据
- 使用Boost.PropertyTree处理XML、JSON和INI数据
- 使用Boost.PropertyTree处理XML、JSON和INI数据 - 做一个优雅的程序员 - 博客频道 - CSDN.NET
- "xxx.xml has more than 80 views, bad for performance"该怎么处理
- C#中采用HTTP方式发送并处理SOAP格式的XML数据
- JAVASCRIPT处理返回的XML字符串
- java之jdom处理xml
- 史上最全web.xml配置文件元素详解
- 利用python处理xml -- 中文编码问题
- 1.4.2 用LINQ to XML 对 XML 文件进行处理
- 使用c#来操作XML中的处理指令(PI)
- 用XPath处理XML时遇到的选取节点的方法
- xml处理模块
- XML解析错误:xml处理指令不在外部实体的开始部分(转)
- 5.Strut国际化动态文本(编程式处理异常)::struts-config.xml web.xml
- xml特殊字符处理 如&
- SQL2005对XML的处理功能讲解
- 对xml的处理_Dom4j入门六
- xstream fromxml时,处理NumberFormatException及忽略多余xml节点的问题
- IDE-DevExpress-JavaScript-JS Framework-Xml处理-Windows Media Player-Components And UI Application Block优秀文章、资料推荐