admin管理员组

文章数量:1609654

linux C语言学习笔记 之 解析xml

<?xml version="1.0" encoding="utf-8"?>
<result>
	<interpretation grammar="session:84c2f34a-9513-42b6-b8f9-a581ebd0299e" confidence="1">
		<instance>
			<result>便民热线</result>
		</instance>
		<input mode="speech">便民热线</input>
		<detail>
			<beginTime>2470</beginTime>
			<endTime>4610</endTime>
			<requestId>a991b9256d524a8bbcb4c66b3b9dc7de</requestId>
			<waveformUri>8c64efcc5bba48e5-1.wav</waveformUri>
		</detail>
	</interpretation>
</result>
void EnumXmlElement(xmlNode * a_node)
{
	//struct msg_xml msgXml;
    xmlNode *cur_node = NULL;
    xmlNode *node = NULL;
    xmlChar *szKey;
	// 遍历节点
	for (cur_node = a_node; cur_node; cur_node = cur_node->next) 
	{
        if (cur_node->type == XML_ELEMENT_NODE) 
		{
			// 打印节点名称
			//printf("<%s>---<%s>===<%s>\n", cur_node->parent,cur_node->name,xmlNodeGetContent(cur_node));
			
			//if(strcmp(cur_node->name,"instance")==0){
			if(!xmlStrcmp(cur_node->name, BAD_CAST "instance")){
				if(strlen(msgXml.result)<=0){
					szKey = xmlNodeGetContent(cur_node);
					char shortStr[1024];
					memset(shortStr,0,1024);
					trim(szKey,shortStr);
					//strcpy(msgXml.result,xmlNodeGetContent(cur_node));
					strcpy(msgXml.result,shortStr);
					//printf("<%s>===<%s>\n",cur_node->name,msgXml.result);
				}
			}
			if(strcmp(cur_node->name,"beginTime")==0){
				if(strlen(msgXml.beginTime)<=0){
					
				}
				strcpy(msgXml.beginTime,xmlNodeGetContent(cur_node));
				//printf("<%s>===<%s>\n",cur_node->name,msgXml.beginTime);
			}
			if(strcmp(cur_node->name,"endTime")==0){
				strcpy(msgXml.endTime,xmlNodeGetContent(cur_node));
			}
			if(strcmp(cur_node->name,"requestId")==0){
				strcpy(msgXml.requestId,xmlNodeGetContent(cur_node));
			}
			if(strcmp(cur_node->name,"waveformUri")==0){
				strcpy(msgXml.waveformUri,xmlNodeGetContent(cur_node));
			}
			// 递归遍历子节点列表
			EnumXmlElement(cur_node->children);
        }
    }
    //printf("<-----<%s>\n",msgXml.result);
    //return msgXml;
}
void main(void){
	if(strstr(msgAll2,"<?xml")){
	   	strcpy(msgFoot,strstr(msgAll2,"<?xml"));
    }
    int len = strlen(msgFoot);
    int k =0;
    if(len>0){
    	xmlDoc *doc = NULL;
		xmlNode *root_element = NULL;
		LIBXML_TEST_VERSION
		doc = xmlParseMemory(msgFoot,len);
		if (doc == NULL) {
			printf("error: could not parse file %s\n", len);
		}
		root_element = xmlDocGetRootElement(doc);
		EnumXmlElement(root_element);
		printf("result-----%s\n",msgXml.result);
		printf("beginTime-----%s\n",msgXml.beginTime);
		printf("endTime-----%s\n",msgXml.endTime);
		printf("requestId-----<%s\n",msgXml.requestId);
		printf("waveformUri-----<%s\n",msgXml.waveformUri);
		xmlFreeDoc(doc);
 	}
    return ;
}

本文标签: 学习笔记语言LinuxXML