工具类库系列(十五)-XmlReader

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 GetNodes(const boost::property_tree::ptree& tag, std::vector<const boost::property_tree::ptree*>& nodes);
			bool GetNodes(const boost::property_tree::ptree& tag, const std::string& childName, std::vector<const boost::property_tree::ptree*>& nodes);
			
			//获取节点属性列表
			bool GetAttrs(const boost::property_tree::ptree& tag, std::vector<const boost::property_tree::ptree*>& attrs);

			//获取某个结点下单一值 <theme>value</theme>
			bool GetValue(const boost::property_tree::ptree& tag, std::string& value, const std::string& default = "");
			bool GetValue(const boost::property_tree::ptree& tag, float& value, const float& default = 0.0f);
			bool GetValue(const boost::property_tree::ptree& tag, int& value, const int& default = 0);
			bool GetValue(const boost::property_tree::ptree& tag, unsigned int& value, const unsigned int& default = 0);

			//获取节点的某个属性值 <theme attr="value"/>
			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, vector<const ptree*>& nodes)
{
	for (auto& itNode : tag)
	{
		if (itNode.first == "<xmlattr>")
		{

		}
		else if (itNode.first == "<xmlcomment>")
		{

		}
		else
		{
			nodes.push_back(&(itNode.second));
		}
	}
	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::GetValue(const ptree& tag, string& value, const string& default)
{
	value = tag.get<string>("", default);
	return true;
}
bool XmlReader::GetValue(const ptree& tag, float& value, const float& default)
{
	value = tag.get<float>("", default);
	return true;
}
bool XmlReader::GetValue(const ptree& tag, int& value, const int& default)
{
	value = tag.get<int>("", default);
	return true;
}
bool XmlReader::GetValue(const ptree& tag, unsigned int& value, const unsigned int& default)
{
	value = tag.get<unsigned int>("", default);
	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;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值