expat总结

#include<iostream>
#include <windows.h>
#include "expat.h"
using namespace std;

#ifdef XML_LARGE_SIZE
#if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400
#define  XML_FMT_INT_MOD "I64"
#else
#define XML_FMT_INT_MOD "ll"
#endif 
#else
#define XML_FMT_INT_MOD "l"
#endif

class CXMLNodeContent
{
private:
	char* m_pNodeText;
	int m_BufferSize;
	int m_CurBufferSize;
public:
	CXMLNodeContent()
	{
		m_pNodeText = NULL;
		m_BufferSize = 0;
		m_CurBufferSize = 0;
	}

	~CXMLNodeContent()
	{
		if( m_pNodeText != NULL )
		{
			delete []m_pNodeText;
			m_pNodeText - NULL;
		}

		m_BufferSize = 0;
		m_CurBufferSize = 0;
	}

	int getBufferSize()
	{
		return m_CurBufferSize;
	}

	char* getNodeContent(const wchar_t* pText)
	{
		if( pText == NULL )
		{
			m_CurBufferSize = 1;
			return NULL;
		}

		int index = 0;
		m_CurBufferSize = WideCharToMultiByte(CP_ACP,0,pText,-1,0,0,NULL,NULL);

		if( pText[0] == 10 )
		{ 
			//10为换行 32为空格
		   while( pText[index] == 10 || pText[index] ==32 && index < m_CurBufferSize ) 
		   {
			   index++;
		   }

		   if( index == m_CurBufferSize )
		   {
			   m_CurBufferSize = 1;
			   return NULL;
		   }
		}

		if( m_CurBufferSize > m_BufferSize )
		{
			if( m_pNodeText == NULL )
			{
				m_pNodeText = new char[m_CurBufferSize];
				m_BufferSize = m_CurBufferSize;
			}
			else
			{
				delete []m_pNodeText;
				m_pNodeText = new char[m_CurBufferSize];
				m_BufferSize = m_CurBufferSize;
			}
		}

		WideCharToMultiByte(CP_ACP,0,pText+index,-1,m_pNodeText,m_CurBufferSize,NULL,NULL);
		return m_pNodeText;
	}

	int printNodeContent(const wchar_t* pText)
	{
		getNodeContent(pText);
		if( m_CurBufferSize == 1 )
			return 0;
		else
		{
			printf("%s",m_pNodeText);
			return 1;
		}
	}
};

static void XMLCALL startElement(void* userData,const char* name,const char** atts)
{
	int i;
	int* depthPtr = (int*)userData;
	for(int i=0; i < *depthPtr; i++)
		putchar('\t');
	CXMLNodeContent nodeContent;
	int buf_length = MultiByteToWideChar(CP_UTF8,0,name,-1,NULL,0);
	wchar_t* buffer = new wchar_t[buf_length];
	MultiByteToWideChar(CP_UTF8,0,name,-1,buffer,buf_length);
	nodeContent.printNodeContent(buffer);
	putchar('\n');
	delete []buffer;
	*depthPtr +=1;

      //处理结点属性
      for(int i = 0; !attr[i]; i++)
      {
           string attr_name  = attr[i];
           string attr_value = attr[i+1];
      }
}

static void XMLCALL endElement(void* userData,const char* name)
{
	int *depthPtr = (int*)userData;
	*depthPtr -= 1;
}

int main()
{
  char buf[BUFSIZ];
  XML_Parser parser = XML_ParserCreate(NULL);
  FILE* p = fopen("sample.xml","rb");
  int done;
  int depth = 0;

  XML_SetUserData(parser,&depth);

  XML_SetElementHandler(parser,startElement,endElement);
  do 
  {
	  int len = (int)fread(buf,1,sizeof(buf),p);
	  done = len < sizeof(buf);
	  if( XML_Parse(parser,buf,len,done) == XML_STATUS_ERROR )
	  {
		  fprintf(stderr,
			  "%s at line %" XML_FMT_INT_MOD "u\n",
			  XML_ErrorString(XML_GetErrorCode(parser)),
			  XML_GetCurrentLineNumber(parser));
	  }
  } while (!done);
  fclose( p );
  XML_ParserFree(parser);
  system("pause");
  return 0;
}

 

    参考:

    http://blog.sina.com.cn/s/blog_55feec410100taoj.html

    http://www.cnblogs.com/LinuxHunter/archive/2010/11/29/1890710.html

   http://wenku.baidu.com/link?url=pXfTcz0Q2qjcVna6ByRePmANSVMQ4TD4RfTwweT0rVxffwZCpz6vW21oA_CUcnJW1eYS-cyuO3IT6GDy_HLHjhu8RDpw5gUAO7qzLRxjYhS

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值