您的位置:首页 > 其它

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{}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: