从简单、实用的角度出发,还是选择tinyXML好。
我在网上找了好几天,只找到2.5.2版的,还是2006年就出了,可能是公司网络有限制,上不了国外网站。不过用着也挺好,在VC6下连接出一个多线程的库,放到别的目录下就能用了(需要三个文件,tinyxml.lib, tinyxml.h,tinystr.h)。
int UnpackXml(char* in_buf, char* out_buf)
{
EnterCriticalSection(&g_cs);
g_iTraceNumber++;
if(g_iTraceNumber > MAX_TRACE_NUMBER)
g_iTraceNumber=1;
LeaveCriticalSection(&g_cs);
// BOOL ret;
CTime t=CTime::GetCurrentTime();
int yea=t.GetYear();
int mon=t.GetMonth();
int day=t.GetDay();
int hou=t.GetHour();
int min=t.GetMinute();
int sec=t.GetSecond();
char buf[100+1]="";
memset(buf, 0, sizeof(buf));
sprintf(buf, "%4d%02d%02d%02d%02d%02d%06d.xml", yea, mon, day, hou, min, sec, g_iTraceNumber);
TiXmlDocument xmlDoc;//strFilePath, 建立一个空对象
xmlDoc.Parse(in_buf);
TiXmlElement* pElmChild = NULL; // 一个指向Element的指针
TiXmlElement* pElmRoot = NULL; // 根节点
pElmRoot = xmlDoc.FirstChildElement( XML_ROOT_ELM ); // 得到根节点
if( !pElmRoot ) {
SaveToLog("未找到xml根节点");
return FALSE;
}
pElmChild = pElmRoot->FirstChildElement( XML_CHILD_ELM_1 ); // 找出第一个
char str_transtype[50]="";
//strcpy(str_transtype, pElmChild->firstChild()->Value());
strcpy(str_transtype, pElmChild->GetText());
//将处理结果写入应答报文,保存为xml, 再复制到out_buf
strcpy(out_buf, str_transtype);
//TiXmlDeclaration Declaration( "1.0","utf-8", "yes" ); // 声明XML的属性
//xmlDoc.InsertEndChild( Declaration ); // 写入基本的XML头结构
//xmlDoc.SaveFile(); // 把XML文件写入硬盘
SaveToLog("UnpackXml Success");
return TRUE;
}
因为我不想在硬盘上建一堆xml文件,所以用了parse, 这也是找了一天才发现的,否则也不好意思贴上来,惭愧!
xml完整内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<trans_data>
<trans_type>xxxx</trans_type>
< source_ip>xxx.xxx.xxx.xxx</ source_ip>
<data>Some words…</data>
</trans_data>
这就可以正常读取xml了,真简单。。。。。。所以不贴出来给博客充实点内容干什么?
VC6咋了?能解决问题就OK。
取之于民,用之于民;
取之于网,用之于网;
是不是很有开源精神?
不佩服我还能佩服谁?