//范例如下,注释随代码
#include <iostream>
#include <string>
#include <map>
using namespace std;
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
using namespace boost::property_tree;
typedef map<string, string> ssMap;
class CBoostXmlUtil{
public:
static ssMap* ReadXml(string& xml){
ptree oTree, oRoot;
xml_parser::read_xml(xml, oTree);
oRoot = oTree.get_child("Root");
ssMap* pConfigMap = new ssMap;
for(ptree::iterator it = oRoot.begin(); it != oRoot.end(); it++){
string strKey = it->first; boost::algorithm::trim(strKey);
ptree oNode = it->second; string strValue = oNode.data(); boost::algorithm::trim(strValue);
pConfigMap->insert(pair<string, string>(strKey, strValue));
}
return pConfigMap;
}
static bool WriteXml(ssMap& mpXml, string& xml){
ptree oNode;
for(ssMap::iterator it = mpXml.begin(); it != mpXml.end(); it++){
oNode.put<string>("Root." + it->first, it->second);//"Root"后的"."不可省略
}
write_xml(xml.c_str(), oNode);
return true;
}
};
/*
<Root>
<User>scott</User>
<Pwd>tigger</Pwd>
<Host>127.0.0.1</Host>
<Port>1521</Port>
</Root>
*/
void Tst01()
{
string sSrcXml("D:\\Projects\\BoostPro\\Debug\\Src.xml");
ssMap* pMap = CBoostXmlUtil::ReadXml(sSrcXml);
if(pMap == NULL){
cout << "Xml Node Not Found !" << endl;
return;
}
for(ssMap::iterator it = pMap->begin(); it != pMap->end(); it++){
cout << it->first << " : " << it->second << endl;
}
cout << endl;
string sDstXml("D:\\Projects\\BoostPro\\Debug\\Dst.xml");
CBoostXmlUtil::WriteXml((*pMap), sDstXml);
delete pMap;
}
/*
<root>
<students>
<name>张三</name>
<age>30</age>
</students>
</root>
*/
//读取固定路径下单一值
void Tst02()
{
ptree oTree;//open xml and read information to oTree;
read_xml("D:\\Projects\\BoostPro\\Debug\\Stu1.xml", oTree);
//read value to var by a path
string sName = oTree.get<string>("root.students.name");
int iAge = oTree.get<int>("root.students.age");
cout << "Name : " << sName << ", Age : " << iAge << endl << endl;
}
/*
<root>
<students>
<name>张三</name>
<name>李四</name>
<name>王五</name>
</students>
</root>
*/
//遍历单层孩子
void Tst03()
{
ptree oTree;//open xml and read information to oTree
read_xml("D:\\Projects\\BoostPro\\Debug\\Stu3.xml", oTree);
ptree oStudents = oTree.get_child("root.students");
for(ptree::iterator it = oStudents.begin(); it != oStudents.end(); it++){
//此时it->first的值为路径名:name
string sName = it->second.get_value<string>();
cout << sName << endl;
}
}
/*
<root>
<student name="张三" age="22">first student</student>
<student name="李四" age="23">second student</student>
<student name="王二" age="24">third student</student>
</root>
*/
//遍历包含属性的孩子
void Tst04()
{
ptree oTree;//open xml and read information to oTree
read_xml("D:\\Projects\\BoostPro\\Debug\\Stu3WithAttr.xml", oTree);
ptree oRoot = oTree.get_child("root");
for(ptree::iterator it = oRoot.begin(); it != oRoot.end(); it++){
ptree oStudent = it->second; //获取列表中的一个学生的节点
cout << oStudent.get<string>("") << endl;//获取值
cout << "Name : " << oStudent.get<string>("<xmlattr>.name");//获取属性
cout << ", Age : " << oStudent.get<string>("<xmlattr>.age") << endl << endl;//获取属性
}
}