c语言环境下的xml数据的序列化的过程以机顶盒向手机APK发送数据为例

前端之家收集整理的这篇文章主要介绍了c语言环境下的xml数据的序列化的过程以机顶盒向手机APK发送数据为例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

机顶盒向手机apk端发送xml格式的数据流程如下所示:

1.调用函数 (1) send_xml_data_to _mobile() ;

2.在函数(1)中调用函数(2)send_common_data_to_mobile();

3.在函数(2)中调用函数(3)create_xml_buff(),并向函数传递了三个重要的参数,分别是压缩前的xml数据缓存xml_send_buff_before_compress,和一个结构体xml_root,以及xml_type;

4.在函数(3)中分情况讨论,调用不同的方法创建携带不同信息的xml文件;

在该项目中携带不同数据信息的xml文件生成主要通过使用libxml2所携带的库函数完成xml文件的创建

以创建频道节目列表为例:

static BOOL create_channel_list_xml(char *result_buffer,void * buf)
{
	xmlTextWriterPtr writer;
	int rc;
	int program_index = 0,j;
	char str[GMS_MAX_BYTE_LENGTH_SERVICE_NAME];
	char buff[GMS_MAX_BYTE_LENGTH_SERVICE_NAME];
	GS_CHANNEL_LIST* channel_list = buf;
	int prog_num;
	GMS_PROGRAM_INFO * xml_array;
	xmlBufferPtr xml_buf = NULL;

	if (result_buffer == NULL)
	{
		return FALSE;
	}
	prog_num = channel_list->request_program_num;
	xml_array = channel_list->program_list;

	xml_buf = xmlBufferCreateSize(PROGRAM_SMALL_XML_BUFF_LENGTH);

	if (xml_buf == NULL)
	{
		return FALSE;
	}
	LIBXML_TEST_VERSION
	writer = xmlNewTextWriterMemory(xml_buf,0);
	if (writer == NULL)
	{
		printf("testXmlwriterFilename: Error creating the xml writer\n");
		return FALSE;
	}
	rc = xmlTextWriterStartDocument(writer,NULL,MY_ENCODING,NULL);

	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
		xmlFreeTextWriter(writer);
		return FALSE;
	}

	//Command
	rc = xmlTextWriterStartElement(writer,BAD_CAST "Command");
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
		xmlFreeTextWriter(writer);
		return FALSE;
	}

	for (program_index = 0; program_index < prog_num; program_index++)
	{
		//parm
		rc = xmlTextWriterStartElement(writer,BAD_CAST "parm");
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//ProgramId
		rc = xmlTextWriterWriteElement(writer,BAD_CAST "ProgramId",BAD_CAST xml_array[program_index].prog_sat_tp_id);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//ProgramName
		convert_windows_to_utf8(xml_array[program_index].prog_name,xml_array[program_index].prog_name,sizeof(xml_array[program_index].prog_name),-1);
		rc = xmlTextWriterWriteElement(writer,BAD_CAST "ProgramName",BAD_CAST xml_array[program_index].prog_name);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//ProgamIndex
		rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "ProgramIndex","%d",xml_array[program_index].prog_index);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//SatName
//			rc = xmlTextWriterWriteElement(writer,BAD_CAST "SatName",BAD_CAST xml_array[i].sat_name);
//			if (rc < 0)
//			{
//				printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
//				xmlFreeTextWriter(writer);
//				return FALSE;
//			}

		//ProgramType
		rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "ProgramType",xml_array[program_index].is_scramble);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//Is HD channel
		rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "IsProgramHD",xml_array[program_index].is_hd);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//FavMark
		rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "FavMark",xml_array[program_index].fav_mark);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//LockMark
		rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "LockMark",xml_array[program_index].lock_mark);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//haveEPG
		rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "HaveEPG",xml_array[program_index].have_epg);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//is_playing
		rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "IsPlaying",xml_array[program_index].is_playing);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//will_be_played
		rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "WillBePlayed",xml_array[program_index].will_be_played);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//CurChannelType
		rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "ChannelType",xml_array[program_index].channel_type);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

//			//freq
//			rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "Frequency",xml_array[i].freq);
//			if (rc < 0)
//			{
//				printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
//				xmlFreeTextWriter(writer);
//				return FALSE;
//			}
//
//			//pol
//			rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "Polar",xml_array[i].pol);
//			if (rc < 0)
//			{
//				printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
//				xmlFreeTextWriter(writer);
//				return FALSE;
//			}

		//modulation_sys
		rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "ModulationSystem",xml_array[program_index].modulation_sys);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//modulation_type
		rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "ModulationType",xml_array[program_index].modulation_type);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//roll_off
		rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "RollOff",xml_array[program_index].roll_off);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//pilot_tones
		rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "PilotTones",xml_array[program_index].pilot_tones);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

//			//sym_rate
//			rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "SymbolRate",xml_array[i].sym_rate);
//			if (rc < 0)
//			{
//				printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
//				xmlFreeTextWriter(writer);
//				return FALSE;
//			}
//
//			//fec
//			rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "Fec",xml_array[i].fec);
//			if (rc < 0)
//			{
//				printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
//				xmlFreeTextWriter(writer);
//				return FALSE;
//			}

		//video_pid
		rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "VideoPid",xml_array[program_index].video_pid);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//audio_pid
		memset(str,0x00,GMS_MAX_BYTE_LENGTH_SERVICE_NAME);
		for (j = 0; j < xml_array[program_index].audio_pid_num; j++)
		{
			if (j > 0)
			{
				string_append(str,GMS_MAX_BYTE_LENGTH_SERVICE_NAME,",");
				itoa(xml_array[program_index].audio_pid[j],buff,10);
				string_append(str,buff);
			}
			else
			{
				itoa(xml_array[program_index].audio_pid[j],buff);
			}
		}
		rc = xmlTextWriterWriteElement(writer,BAD_CAST "AudioPid",BAD_CAST str);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//ttx_pid
		rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "TtxPid",xml_array[program_index].ttx_pid);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//sub_pid
		memset(str,GMS_MAX_BYTE_LENGTH_SERVICE_NAME);
		for (j = 0; j < xml_array[program_index].sub_pid_num; j++)
		{
			if (j > 0)
			{
				string_append(str,");
				itoa(xml_array[program_index].sub_pid[j],buff);
			}
			else
			{
				itoa(xml_array[program_index].sub_pid[j],BAD_CAST "SubPid",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//pmt_pid
		rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "PmtPid",xml_array[program_index].pmt_pid);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//which tunner
		rc = xmlTextWriterWriteFormatElement(writer,BAD_CAST "IsTuner2",xml_array[program_index].is_channel_in_tunner_2);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterWriteElement LINE:%d\n",__LINE__);
			xmlFreeTextWriter(writer);
			return FALSE;
		}

		//end of parm
		rc = xmlTextWriterEndElement(writer);
		if (rc < 0)
		{
			printf("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
			xmlFreeTextWriter(writer);
			return FALSE;
		}
	}

	rc = xmlTextWriterEndElement(writer);
	if (rc < 0)
	{
		printf("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
		xmlFreeTextWriter(writer);
		return FALSE;
	}

	xmlFreeTextWriter(writer);
	strcpy(result_buffer,(const char *)xml_buf->content);
	xmlCleanupParser();
	xmlBufferFree(xml_buf);
	return TRUE;
}

猜你在找的XML相关文章