采用libxml2解析xml资源
Posted alen_xie
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了采用libxml2解析xml资源相关的知识,希望对你有一定的参考价值。
通过分析xml资源,发现它不是一个标准的xml资源,通过调用xml解析
xmlXPathObjectPtr getNodeset(xmlDocPtr doc, const xmlChar *xpath)
xmlXPathContextPtr context;
xmlXPathObjectPtr result;
context = xmlXPathNewContext(doc);
if (context == NULL)
printf("context is NULL, [%s, %d]\\n", __FUNCTION__, __LINE__);
return NULL;
result = xmlXPathEvalExpression(xpath, context);
xmlXPathFreeContext(context);
if (result == NULL)
printf("xmlXPathEvalExpression return NULL, [%s, %d]\\n", __FUNCTION__, __LINE__);
return NULL;
if (xmlXPathNodeSetIsEmpty(result->nodesetval))
xmlXPathFreeObject(result);
printf("nodeset is empty, [%s, %d]\\n", __FUNCTION__, __LINE__);
return NULL;
return result;
xmlXPathObjectPtr getNodeset(xmlDocPtr doc, const xmlChar *xpath);
BOOL ParseFreeiIptvEpgInforFunc(char *pcDownData, int iGetTotalLen, FREEI_IPTV_EPG *pstOTTData)
int iIndex = 0;
BOOL bRetuFlg = FALSE;
xmlDocPtr doc;
xmlXPathObjectPtr xml_entry = NULL;
xmlChar *xpath = BAD_CAST("/tv/programme");
if (pcDownData == NULL)
printf ("[%s, %d] not malloc xml memory\\n", __FUNCTION__, __LINE__);
return bRetuFlg;
doc = xmlReadMemory(pcDownData, iGetTotalLen, "noname.xml", NULL, 0);
if (doc == NULL)
return bRetuFlg;
xml_entry = getNodeset(doc, xpath);
if (xml_entry)
xmlNodeSetPtr nodeset = xml_entry->nodesetval;
xmlNodePtr cur;
xmlAttrPtr attrPtr;
pstOTTData->pstFreIvEpgList = (FREEI_IPTV_EPG_LIST *)GSOS_Malloc(sizeof(FREEI_IPTV_EPG_LIST)*(nodeset->nodeNr));
if (NULL == pstOTTData->pstFreIvEpgList)
return bRetuFlg;
pstOTTData->iEPGTotal = nodeset->nodeNr;
for (iIndex=0; iIndex < nodeset->nodeNr; iIndex++)
cur = nodeset->nodeTab[iIndex];
attrPtr = cur->properties;
while(NULL != attrPtr)
if (!xmlStrcmp(attrPtr->name, BAD_CAST "start"))
xmlChar* szAttr = xmlGetProp(cur, BAD_CAST "start");
snprintf (pstOTTData->pstFreIvEpgList[iIndex].acStartTime, \\
sizeof(pstOTTData->pstFreIvEpgList[iIndex].acStartTime), "%s", szAttr);
xmlFree(szAttr);
if (!xmlStrcmp(attrPtr->name, BAD_CAST "stop"))
xmlChar* szAttr = xmlGetProp(cur, BAD_CAST "stop");
snprintf (pstOTTData->pstFreIvEpgList[iIndex].acStopTime, \\
sizeof(pstOTTData->pstFreIvEpgList[iIndex].acStopTime), "%s", szAttr);
xmlFree(szAttr);
attrPtr = attrPtr->next;
cur = cur->xmlChildrenNode;
while (NULL != cur)
if (!xmlStrcmp(cur->name, (const xmlChar *)"title"))
snprintf (pstOTTData->pstFreIvEpgList[iIndex].title, \\
sizeof(pstOTTData->pstFreIvEpgList[iIndex].title), "%s", \\
((char*)XML_GET_CONTENT(cur->xmlChildrenNode)));
else if (!xmlStrcmp(cur->name, (const xmlChar *)"desc"))
snprintf (pstOTTData->pstFreIvEpgList[iIndex].desc, \\
sizeof(pstOTTData->pstFreIvEpgList[iIndex].desc), "%s",\\
((char*)XML_GET_CONTENT(cur->xmlChildrenNode)));
cur = cur->next;
xmlXPathFreeObject(xml_entry);
bRetuFlg = TRUE;
return bRetuFlg;
以上是关于采用libxml2解析xml资源的主要内容,如果未能解决你的问题,请参考以下文章