#include <libxml/parser.h>
/**编码转换函数***/
char *ConvertEnc(char *encFrom, char *encTo, const char * in)
{
static char bufin[1024], bufout[1024], *sin, *sout;/**静态局部变量**/
int mode, lendin, lenout, ret, nline;
iconv_t c_pt;
if( (c_pc = iconv_open(encTo, encFrom) ) == (iconv_t) - 1 )/**申请一个转换描述符**/
{
printf("incov_open error/n");
return NULL;
}
iconv(c_pc,NULL, NULL, NULL, NULL);/**转换状态恢复至初始状态**/
lenin = strlen(in) + 1;
lenout = 1024;
sin = (char *)in;
sout = bufout;
ret = iconv(c_pc,&sin, (size_t *)&lenin, &sout, (*size_t)&lenout);
/***函数原型:size_t iconv (iconv_t cd,const char* * inbuf, size_t * inbytesleft, char* * outbuf, size_t * outbytesleft);****/
if(ret == -1)
{
return NULL;
}
iconv_close(c_pt);
return bufout;
}
/**解析xml**/
int childStory(xmlNodePtr cur, char filename[21], char edition[5])
{
xmlAttrPtr attrPtr;
xmlChar *szAttr;
char *tmp_szattr;
char *attr;
while( NULL != cur )
{
if( !(xmlStrcmp(cur->name,(const xmlChar *)"text"))))
{
cur = cur->next;
continue;
}
attrPtr = cur->properties;
attr = NULL;
attr = (char *)attrPtr->name;
szAttr = xmlGetProp(cur, BAD_CAST, attrPtr->name);
tmp_szattr = NULL;
tmp_szattr = ConvertEnc("utf-8","gb2312",(char *)szAttr);
if(tmp_szattr == NULL)
tmp_szattr = szAttr;
xmlFree(szAttr);
attrPtr = attrPtr->next;
cur = cur->next;
}
return 0;
}
int main()
{
xmlDocPtr doc;
xmlNodePtr curNode;
doc =xmlReadFile(szDocName,"utf-8", XML_PARSE_RECOVER);/**szDocName=完整路径文件名**/
if(NULL == doc)
{
}
curNode = xmlDocGerRootElement(doc);/**获取根节点元素**/
if(NULL == curNode)
{
}
if(xmlStrcmp(curNode->name, BAD_CAST "root"))
{
}
if(childStory(curNode->xmlChildernNode,ls_filename,ls_edition)<0) /***lsfilename文件名称,edition只是个日期/
{
strncpy(mvDocName,szDocName,strlen(szDocName));
strcat(mvDocName,".bad");
}
else
{
strncpy(mvDocName,szDocName,strlen(szDocName));
strcat(mvDocName,".bak");
}
xmlFreeDoc(doc);
}