tinyXML解析
本帖最后由 18225629625 于 2016-05-11 16:53 编辑最近项目中使用C、C++混编,需要解析XML,操作的流程是读取本地的XML文件,获取buf,然后使用tinyXML解析
操作如下:
//根据数据中心传过来的XMLBuf获得本模块需要的UpdataTime
extern "C"void OHSERVER_InitModule(char *pBuffer)
{
C_DEBUG;
//将XML存储到本模块的BUFFER中
char xmlbuf = "";
strcpy(xmlbuf, pBuffer);
XMLHelper OHxmlHelp(xmlbuf);
NodeInfor nodeInfor;
ClearNodeVar();
//UpdataTime
OHxmlHelp.ReadNode((char *)"root/OHCloud/Time", nodeInfor);
UpdataTime = atoi(OHxmlHelp.EasyReadNodeAttributeValue(nodeInfor, "TimeUpData").c_str());
//UpdataTime = 10;
printf("UpdataTime = %d\n", UpdataTime);
}
但是有的时候会在创建对象的时候就提示下列错误:
“void TixmlParsingData::Stamp(const char *, TixmlEncoding): Assertion `cursor.col >= -1` failed。
XMLHelper的构造函数如下:
XMLHelper::XMLHelper(char* pbuf)
{
m_pbuffer = new char;
memset(m_pbuffer,0,1024*100);
if(pbuf)
strcpy(m_pbuffer,pbuf);
else
strcpy(m_pbuffer,"<?xml version=\"1.0\" encoding=\"GBK\"?>");
m_pDoc = new TiXmlDocument(m_pbuffer);
}
再往下追踪到发生错误的地方,功力太低,搞不明白了
void TiXmlParsingData::Stamp( const char* now, TiXmlEncoding encoding )
{
assert( now );
// Do nothing if the tabsize is 0.
if ( tabsize < 1 )
{
return;
}
// Get the current row, column.
int row = cursor.row;
int col = cursor.col;
const char* p = stamp;
assert( p );
while ( p < now )
{
// Treat p as unsigned, so we have a happy compiler.
const unsigned char* pU = (const unsigned char*)p;
// Code contributed by Fletcher Dunn: (modified by lee)
switch (*pU) {
case 0:
// We *should* never get here, but in case we do, don't
// advance past the terminating null character, ever
return;
case '\r':
// bump down to the next line
++row;
col = 0;
// Eat the character
++p;
// Check for \r\n sequence, and treat this as a single character
if (*p == '\n') {
++p;
}
break;
case '\n':
// bump down to the next line
++row;
col = 0;
// Eat the character
++p;
// Check for \n\r sequence, and treat this as a single
// character.(Yes, this bizarre thing does occur still
// on some arcane platforms...)
if (*p == '\r') {
++p;
}
break;
case '\t':
// Eat the character
++p;
// Skip to next tab stop
col = (col / tabsize + 1) * tabsize;
break;
case TIXML_UTF_LEAD_0:
if ( encoding == TIXML_ENCODING_UTF8 )
{
if ( *(p+1) && *(p+2) )
{
// In these cases, don't advance the column. These are
// 0-width spaces.
if ( *(pU+1)==TIXML_UTF_LEAD_1 && *(pU+2)==TIXML_UTF_LEAD_2 )
p += 3;
else if ( *(pU+1)==0xbfU && *(pU+2)==0xbeU )
p += 3;
else if ( *(pU+1)==0xbfU && *(pU+2)==0xbfU )
p += 3;
else
{ p +=3; ++col; } // A normal character.
}
}
else
{
++p;
++col;
}
break;
default:
if ( encoding == TIXML_ENCODING_UTF8 )
{
// Eat the 1 to 4 byte utf8 character.
int step = TiXmlBase::utf8ByteTable[*((const unsigned char*)p)];
if ( step == 0 )
step = 1; // Error case from bad encoding, but handle gracefully.
p += step;
// Just advance one column, of course.
++col;
}
else
{
++p;
++col;
}
break;
}
}
cursor.row = row;
cursor.col = col;
assert( cursor.row >= -1 );
assert( cursor.col >= -1 );
stamp = p;
assert( stamp );
小弟XML解析刚刚接触,请大虾们帮帮忙 这是 xml 库的源码吗?感觉没必要直接看源码,看接口怎么用就行了。
嗯,是的,采用的是封装好的函数接口,只不过在使用的过程中,偶尔会出现上述的错误,有点无从下手的感觉,所以求助下,看看大家有没有出现过同样的问题回复 2# VIP_fuck
谢谢大侠相助,小弟收下了:D 回复 5# BetonArmEE
页:
[1]