1 <?xml version="1.0" encoding="utf-8"?> 2 <Config> 3 <Item name="IP测试报告2017-10-24 09-54-31"> 4 <ChildItem name="Date" desc="" datatype ="string">2017-10-24 09-54-31</ChildItem> 5 <ChildItem name="FailureCount" desc="" datatype ="int">1</ChildItem> 6 <ChildItem name="IPAddressCount" desc="" datatype ="int">4</ChildItem> 7 <ChildItem name="SuccessCount" desc="" datatype ="int">3</ChildItem> 8 </Item> 9 </Config>
如何生成以上xml
for (map<wstring, map<wstring, HistoryData>>::iterator it = vect_str.begin(); it != vect_str.end(); it++) { //迭代vector for (map<wstring, HistoryData>::iterator it1 = it->second.begin(); it1 != it->second.end(); it1++) { m_pt3.add(L"<xmlattr>.name", it1->second.name); m_pt3.add(L"<xmlattr>.desc", it1->second.desc); m_pt3.add(L"<xmlattr>.datatype ", it1->second.datatype); m_pt3.put_value(it1->second.value); m_pt2.add_child(L"ChildItem", m_pt3); m_pt3.clear(); } m_pt1.add_child(L"Item", m_pt2); m_pt1.add(L"Item.<xmlattr>.name", it->first); m_pt2.clear();
} m_pt.add_child(L"Config", m_pt1);
auto settings = boost::property_tree::xml_writer_make_settings<std::wstring>(L'\t', 1);
write_xml(fileName, m_pt, utf8Locale, settings);
如何在现有的xml里插入新的Item如下
<?xml version="1.0" encoding="utf-8"?> <Config> <Item name="IP测试报告2017-10-24 09-54-31"> <ChildItem name="Date" desc="" datatype="string">2017-10-24 09-54-31</ChildItem> <ChildItem name="FailureCount" desc="" datatype="int">1</ChildItem> <ChildItem name="IPAddressCount" desc="" datatype="int">4</ChildItem> <ChildItem name="SuccessCount" desc="" datatype="int">3</ChildItem> </Item> <Item name="IP测试报告2017-10-24 09-54-32"> <ChildItem name="Date" desc="" datatype ="string">2017-10-24 09-54-32</ChildItem> <ChildItem name="FailureCount" desc="" datatype ="int">1</ChildItem> <ChildItem name="IPAddressCount" desc="" datatype ="int">4</ChildItem> <ChildItem name="SuccessCount" desc="" datatype ="int">3</ChildItem> </Item> </Config>
代码
if (auto firstChild = m_pt.get_child_optional(L"Config")) { m_pt4 = m_pt.get_child(L"Config"); m_pt.clear(); for (map<wstring, map<wstring, HistoryData>>::iterator it = vect_str.begin(); it != vect_str.end(); it++) { //迭代vector for (map<wstring, HistoryData>::iterator it1 = it->second.begin(); it1 != it->second.end(); it1++) { m_pt3.add(L"<xmlattr>.name", it1->second.name); m_pt3.add(L"<xmlattr>.desc", it1->second.desc); m_pt3.add(L"<xmlattr>.datatype ", it1->second.datatype); m_pt3.put_value(it1->second.value); m_pt2.add_child(L"ChildItem", m_pt3); m_pt3.clear(); } m_pt2.add(L"<xmlattr>.name", it->first); m_pt4.add_child(L"Item", m_pt2); m_pt2.clear(); } m_pt.add_child(L"Config", m_pt4); } else { for (map<wstring, map<wstring, HistoryData>>::iterator it = vect_str.begin(); it != vect_str.end(); it++) { //迭代vector for (map<wstring, HistoryData>::iterator it1 = it->second.begin(); it1 != it->second.end(); it1++) { m_pt3.add(L"<xmlattr>.name", it1->second.name); m_pt3.add(L"<xmlattr>.desc", it1->second.desc); m_pt3.add(L"<xmlattr>.datatype ", it1->second.datatype); m_pt3.put_value(it1->second.value); m_pt2.add_child(L"ChildItem", m_pt3); m_pt3.clear(); } m_pt1.add_child(L"Item", m_pt2); m_pt1.add(L"Item.<xmlattr>.name", it->first); m_pt2.clear(); } m_pt.add_child(L"Config", m_pt1); } auto settings = boost::property_tree::xml_writer_make_settings<std::wstring>(L'\t', 1); write_xml(fileName, m_pt, utf8Locale, settings);