C++ 写 xml

0 篇文章 0 订阅
//
//
// XMLWrite.h
// 
// Version: V1.0
//
// Author: Dennis, May 2010
// 
// This code is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
// History:
// 2011-04-03 Dennis      1.set TAB_SPACE vale 4
//                        2.modify cout statement from "stackTag.top().tag"
//                          to "stackTag.top().tag.c_str()"
//                        3.modify WriteComment function, make sure
//                          comment start with a new line
//

#ifndef __KMLWRITE_H__
#define __KMLWRITE_H__

//use for ofstream, fstream use for open file to read/write
#include <fstream>

#include <stack>
using namespace std;

#define INDENT_FIRSTTAG 0	// indent of first tag text
#define TAB_SPACE 4			// number of spaces for Tab key

class XMLWrite
{
public:
	XMLWrite(ostream& _out):out(_out),m_bIsEnter(false),m_bIsText(false){}
	~XMLWrite(void){}

	inline void WriteProlon()
	{
		out<<"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
		m_bIsEnter = true;
	}

	inline void WriteComment(const char* pComment)
	{
		if (!m_bIsEnter)
		{
			out<<endl;
		}

		out<<"<!--"<<pComment<<"-->"<<endl;
		m_bIsEnter = true;
	}

	void WriteTag(const char* pTag, bool bIsExistAttribute = false)
	{
		if (!m_bIsEnter)
		{
			out<<endl;
		}

		//calculate indent of each tag
		XMLTag tag;
		if (0 == stackTag.size())
		{
			tag.tag = pTag;
			tag.nTabCount = INDENT_FIRSTTAG;
			stackTag.push(tag);
		}
		else
		{
			tag.tag = pTag;
			tag.nTabCount = stackTag.top().nTabCount+TAB_SPACE;
			stackTag.push(tag);
		}

		int nTabCount = 0;
		nTabCount = stackTag.top().nTabCount;
		while (nTabCount-- > 0)
		{
			out<<" ";
		}

		if (bIsExistAttribute)
		{
			out<<"<"<<pTag<<" ";
		}
		else
		{
			out<<"<"<<pTag<<">";
		}
		m_bIsEnter = false;
	}

	void WriteAttribute(const char* pAttributeName, const char* pValue, bool bCloseTag)
	{
		out<<pAttributeName<<"=\""<<pValue<<"\"";
		if (bCloseTag)
		{
			out<<">"<<endl;
			m_bIsEnter = true;
		}
		else
		{
			out<<" ";
		}
	}

	void WriteText(const char* pStr,bool bIsMulti = false)
	{
 		if (bIsMulti)
 		{
			out<<endl;
 			int nTabCount = stackTag.top().nTabCount+TAB_SPACE;
 			while(nTabCount-- > 0)
 			{
 				out<<" ";
 			}
 		}

		m_bIsMulti = bIsMulti;

		out<<pStr;

		m_bIsEnter = false;
		m_bIsText = true;
	}

	//@ nFlag -1:write all end flag, n: write n end flag
	void WriteEndTag(int nNumber = 1)
	{
		//if nNumber=-1, close all end tag
		if (-1 == nNumber)
		{
			nNumber = (int)stackTag.size();
		}

		//if previous string is not text, write enter symbol
		if (!m_bIsText && !m_bIsEnter)
		{
			out<<endl;
		}

		if (stackTag.empty())
		{
			return;
		}

		if (m_bIsMulti)
		{
			out<<endl;
		}

		//write top of stack
		int nTabCount = stackTag.top().nTabCount;
		while (nTabCount-- > 0)
		{
			if (m_bIsText && !m_bIsMulti)
			{
				break;
			}
			out<<" ";
		}
		out << "</" << stackTag.top().tag.c_str() << '>' <<endl;
		stackTag.pop();

		//write remain element
		while (!stackTag.empty() && 0 != --nNumber)
		{
			nTabCount = stackTag.top().nTabCount;
			while (nTabCount-- > 0)
			{
				out<<" ";
			}
			out << "</" << stackTag.top().tag.c_str() << '>' <<endl;
			stackTag.pop();
		}
		
		m_bIsText = false;
		m_bIsEnter = true;
		m_bIsMulti = false;
	}

protected:
private:
	ostream& out;
	
	bool m_bIsEnter;//Check whether the previous character is enter symbol
	bool m_bIsText;	//Check whether the previous string is text
	bool m_bIsMulti;//flag of multi line text

	typedef struct  XMLTag
	{
		string tag;		//name of tag
		int nTabCount;	//count of tab symbols
	} XMLTag;

	stack<XMLTag> stackTag;	//stack of tag
};

#endif


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值