#include "pch.h"
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
using namespace std;
int main()
{
string path = "C:\\Work\\Students.xml";
string path2 = "C:\\Work\\Students2.xml";
try
{
std::cout << "Using property tree to manipulate XML files\n";
//how to load an xml file
boost::property_tree::ptree pt;
read_xml(path, pt);
boost::property_tree::ptree& Students = pt.get_child("Students");
//how to remove a node using Id
for (auto it = Students.begin(); it != Students.end();)
{
int id = it->second.get(".Id");
if (id == 1)
{
it = Students.erase(it);
}
else
{
++it;
}
}
//how to add nodes
for (auto data : { 3, 4, 5 })
{
boost::property_tree::ptree& Student = Students.add("Student", "");
Student.add(".Id", data);
Student.add("LastName", "McDonalds");
Student.add("FirstName", "Robert");
}
//how to save
write_xml(path2, pt, std::locale(), boost::property_tree::xml_writer_make_settings('\t', 1));
}
catch (exception e)
{
string s = e.what();
std::cout << s;
}
}
14 April 2019
C++ Use Boost property_tree to manipulate xml files
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment