关闭

C++ 写 xml

标签: c++stringcharacterencodingstructfunction
630人阅读 评论(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


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:134545次
    • 积分:1195
    • 等级:
    • 排名:千里之外
    • 原创:30篇
    • 转载:11篇
    • 译文:0篇
    • 评论:26条
    最新评论