XmlReader是用来支持xml文件读取的一个工具类
很简单,也就是封装一下boost的property_tree,
提供两类接口,获取xml结点的属性,获取xml结点的子节点列表
XmlReader.h
#ifndef __XmlReader_h__
#define __XmlReader_h__
#include <string>
#include <vector>
#include <boost/property_tree/ptree.hpp>
namespace common {
namespace tool {
class XmlReader
{
public:
XmlReader();
~XmlReader();
//加载xml文件
bool LoadFile(const std::string& pathName);
//获取根节点
const boost::property_tree::ptree& GetRoot();
//获取节点属性列表
bool GetAttrs(const boost::property_tree::ptree& tag, std::vector<const boost::property_tree::ptree*>& attrs);
//获取子节点列表
bool GetNodes(const boost::property_tree::ptree& tag, const std::string& childName, std::vector<const boost::property_tree::ptree*>& nodes);
//获取节点的某个属性值
bool GetAttr(const boost::property_tree::ptree& tag, const std::string& attrName, std::string& value, const std::string& default = "");
bool GetAttr(const boost::property_tree::ptree& tag, const std::string& attrName, float& value, const float& default = 0.0f);
bool GetAttr(const boost::property_tree::ptree& tag, const std::string& attrName, int& value, const int& default = 0);
bool GetAttr(const boost::property_tree::ptree& tag, const std::string& attrName, unsigned int& value, const unsigned int& default = 0);
private:
boost::property_tree::ptree m_Pt;
};
}
}
#endif
XmlReader.cpp
#include "XmlReader.h"
#include <string>
#include <vector>
#include <fstream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
using namespace common::tool;
using namespace std;
using namespace boost::property_tree;
XmlReader::XmlReader()
{
}
XmlReader::~XmlReader()
{
}
bool XmlReader::LoadFile(const string& pathName)
{
ifstream file(pathName.c_str());
if (file)
{
xml_parser::read_xml(file, m_Pt);
return true;
}
else
{
return false;
}
}
const ptree& XmlReader::GetRoot()
{
return m_Pt;
}
bool XmlReader::GetAttrs(const ptree& tag, vector<const ptree*>& attrs)
{
for (auto& itNode : tag)
{
if (itNode.first == "<xmlattr>")
{
attrs.push_back(&(itNode.second));
}
else if (itNode.first == "<xmlcomment>")
{
}
else
{
}
}
return true;
}
bool XmlReader::GetNodes(const ptree& tag, const string& childName, vector<const ptree*>& nodes)
{
for (auto& itNode : tag)
{
if (itNode.first == "<xmlattr>")
{
}
else if (itNode.first == "<xmlcomment>")
{
}
else
{
if (itNode.first == childName)
{
nodes.push_back(&(itNode.second));
}
}
}
return true;
}
bool XmlReader::GetAttr(const ptree& tag, const string& attrName, string& value, const string& default)
{
value = tag.get<string>("<xmlattr>." + attrName, default);
return true;
}
bool XmlReader::GetAttr(const ptree& tag, const string& attrName, float& value, const float& default)
{
value = tag.get<float>("<xmlattr>." + attrName, default);
return true;
}
bool XmlReader::GetAttr(const ptree& tag, const string& attrName, int& value, const int& default)
{
value = tag.get<int>("<xmlattr>." + attrName, default);
return true;
}
bool XmlReader::GetAttr(const ptree& tag, const string& attrName, unsigned int& value, const unsigned int& default)
{
value = tag.get<unsigned int>("<xmlattr>." + attrName, default);
return true;
}