⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlstringlate.cpp

📁 ML DOM (文档对象模型)对象提供了一个标准的方法来操作存储在XML文档中的信息
💻 CPP
字号:
// XMLStringTranslate.cpp: implementation of the XMLStringTranslate class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "XMLStringTranslate.h"

#include <xercesc/util/XMLString.hpp>
#include <xercesc\util\platformutils.hpp>
#include <xercesc\util\transservice.hpp>


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

XMLStringTranslate::XMLStringTranslate(const char * const encoding):fFormatter(0),
	m_value(0),fEncodingUsed(0),toFill(0)
{	
  //  XMLFormatTarget * myFormTarget = new XMLStringTranslate;
	fEncodingUsed=XMLString::transcode(encoding);
	fFormatter = new XMLFormatter(fEncodingUsed
                                     ,this
                                     ,XMLFormatter::NoEscapes
                                     ,XMLFormatter::UnRep_CharRef);
	toFill=new XMLCh[kTmpBufSize];
	clearbuffer();
}

XMLStringTranslate::~XMLStringTranslate()
{
	if(fFormatter)
		delete fFormatter;
	if(fEncodingUsed)
		delete [] fEncodingUsed;
	if(m_value)
		free(m_value);	
	if(toFill)
		free(toFill);
	
	fFormatter=0;
	fEncodingUsed=0;
	m_value=0;
	toFill=0;
}

void XMLStringTranslate::writeChars(const XMLByte* const  toWrite
                                  , const unsigned int    count
                                  , XMLFormatter* const   formatter)
{
        // Surprisingly, Solaris was the only platform on which
        // required the char* cast to print out the string correctly.
        // Without the cast, it was printing the pointer value in hex.
        // Quite annoying, considering every other platform printed
        // the string with the explicit cast to char* below.
///    cout.write((char *) toWrite, (int) count);
  //  cout.flush();
	if(m_value)
		free(m_value);
	m_value=0;
	m_value=new char[count+1];
	memset(m_value,0,count+1);
	memcpy(m_value,(char *)toWrite,count+1);	
}
void XMLStringTranslate::clearbuffer()
{
	if(!toFill)
		return;
	for(int i=0;i<kTmpBufSize;i++)
		toFill[i]=0;
}
string XMLStringTranslate::translate(const XMLCh* const value)
{
	*fFormatter<<value;	
	string strValue=string(m_value);
	return strValue;
}

// Function name	: XMLStringTranslate::translate
// Description	    : 将字符串value转换为XMLCh
// Return type		: XMLCh * 转换过的字符窜,注意内存释放
// Argument         : const char * const value
const XMLCh * const XMLStringTranslate::translate(const char * const value)
{	
	clearbuffer();
	const unsigned int  srcCount=XMLString::stringLen(value);
	unsigned char fCharSizeBuf[kCharBufSize];
	XMLTranscoder * pTranscoder=(XMLTranscoder *)fFormatter->getTranscoder();		
	unsigned int bytesEaten;
	unsigned int size=pTranscoder->transcodeFrom
	(
		(XMLByte *)value,
		srcCount,
		toFill,
		kTmpBufSize,
		bytesEaten,
		fCharSizeBuf
	);
//	toFill[size]=0;	
	string t1=string(value);
	string t2=translate(toFill);
	assert(t1==t2);
	return toFill;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -