今天在工作中遇到了一个需要处理xml格式的字符串,需求是修改某个固定的value值,并且还要把这个xml的key和value按照原本的格式在推送回去。

如果使用库,就显得太臃肿了,就想写个简单的demo实现这个功能:

/*
AnalysisXml.c
*/
#include<stdio.h>
#include<string.h> /* function: find the corresponding value with the key transmitted
* parameter :
* [in/out] pTmpXml the afferent string of xml that remaining after finding
* [int]pKey the key of incoming
* [out]pValue find the corresponding value
* returned value:
* -1 : pStrXml or pStuXml is null
* -2 : the corresponding value is null or the key is not find
*/
static int AnalysisXml(char *pTmpXml, char *pValue, char *pKey)
{
int iRet = -;
if (pTmpXml == NULL || pValue == NULL || pKey == NULL)
{
return iRet;
} memset(pValue, , sizeof(pValue));
char BegKey[] = {};
char EndKey[] = {};
sprintf(BegKey, "<%s>", pKey);
sprintf(EndKey, "</%s>", pKey); //printf("BegKey = %s\n", BegKey);
//printf("EndKey = %s\n", EndKey); char *pBegin = pTmpXml;
char *pEnd = NULL; int BegKey_len = strlen(BegKey); if ( (pBegin = strstr(pTmpXml, BegKey)) != NULL)
{ pEnd = strstr(pBegin + ,EndKey );
memcpy(pValue, pBegin + BegKey_len, pEnd - pBegin - BegKey_len);
//printf("%s = %s\n", BegKey, pValue);
pTmpXml = pEnd + strlen(EndKey);
//printf("pTmpXml = %s\n",pTmpXml);
iRet = ;
}
else
{
iRet = -;
} return iRet; } /* function: analysis string whose foramt is xml
* parameter :
* [in] pStrXml the afferent string of xml
* [out]pStuXml the struct of saving value of various
* returned value:
* -1 : pStrXml or pStuXml is null
*/
int HandleXml(char *pStrXml, XML_CONFIG_INPUT *pStuXml)
{
int iRet = -;
if (pStrXml == NULL || pStuXml == NULL)
{
return iRet; //
} char szInbuf[] = {};
int nInbufSize = ;
nInbufSize += sprintf(szInbuf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); char key[] = {};
char value[] = {};
char *pTmpXml = pStrXml; char KeyXml[][] = {"id", "maxFrameRate", "reflectiveEnable", "reflectiveTemperature",
"emissivity", "distance", "refreshInterval", "distanceUnit",
"temperatureDataLength", "jpegPicEnabled"};
int len_KeyXml = sizeof(KeyXml) / sizeof(KeyXml[]);
printf("len_KeyXml = %d\n", len_KeyXml);
for (int i = ; i < len_KeyXml; i++)
{
//printf("KeyXml[%d] = %s\n", i, KeyXml[i]);
sprintf(key, "%s", KeyXml[i]);
iRet = AnalysisXml(pTmpXml, value, key); if (value != NULL)
{
printf("%s = %s\n", key, value);
bool flag = (strcmp(key, "jpegPicEnabled"));
if(!flag)
{
nInbufSize += sprintf(szInbuf+nInbufSize, "\n<JpegPictureWithAppendData>");
}
nInbufSize += sprintf(szInbuf+nInbufSize, "\n<%s>%s</%s>", key, value, key);
if(!flag)
{
nInbufSize += sprintf(szInbuf+nInbufSize, "\n</JpegPictureWithAppendData>");
}
}
else
{
printf("value is null\n");
}
} printf("\nszInbuf = %s \n",szInbuf);
return ;
} int main()
{
int iRet = -;
char Outxml[] = " <?xml version=\"1.0\" encoding=\"UTF-8\"?> \n\
<PixelToPixelParam version=\"2.0\" xmlns=\"http://www.hikvision.com/ver20/XMLSchema\"> \n\
<id></id> \n\
<maxFrameRate></maxFrameRate> \n\
<reflectiveEnable>false</reflectiveEnable> \n\
<reflectiveTemperature>20.00</reflectiveTemperature>\n\
<emissivity>0.96</emissivity>\n\
<distance></distance>\n\
<refreshInterval></refreshInterval>\n\
<distanceUnit>centimeter</distanceUnit>\n\
<temperatureDataLength></temperatureDataLength>\n\
<JpegPictureWithAppendData>\n\
<jpegPicEnabled>true</jpegPicEnabled>\n\
</JpegPictureWithAppendData>\n\
</PixelToPixelParam>"; char Inxml[] = {}; float fEmissvity = 0.01;
int wDistance = ;
//iRet = HandleXml(Outxml, &stuXml); char szInbuf[] = {};
int nInbufSize = ;
nInbufSize += sprintf(szInbuf, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); char key[] = {};
char value[] = {};
char *pTmpXml = Outxml; char KeyXml[][] = {"id", "maxFrameRate", "reflectiveEnable", "reflectiveTemperature",
"emissivity", "distance", "refreshInterval", "distanceUnit",
"temperatureDataLength", "jpegPicEnabled"};
int len_KeyXml = sizeof(KeyXml) / sizeof(KeyXml[]);
printf("len_KeyXml = %d\n", len_KeyXml);
for (int i = ; i < len_KeyXml; i++)
{
//printf("KeyXml[%d] = %s\n", i, KeyXml[i]);
sprintf(key, "%s", KeyXml[i]);
iRet = AnalysisXml(pTmpXml, value, key); if (value != NULL)
{
printf("%s = %s\n", key, value);
bool flag = (strcmp(key, "jpegPicEnabled"));
if(!flag)
{
nInbufSize += sprintf(szInbuf+nInbufSize, "\n<JpegPictureWithAppendData>");
} if ( == (strcmp(key, "emissivity")))
{
nInbufSize += sprintf(szInbuf+nInbufSize, "\n<%s>%.2f</%s>", key, fEmissvity, key);
}
else if ( == (strcmp(key, "distance")))
{
nInbufSize += sprintf(szInbuf+nInbufSize, "\n<%s>%d</%s>", key, wDistance, key);
}
else
{
nInbufSize += sprintf(szInbuf+nInbufSize, "\n<%s>%s</%s>", key, value, key);
} if(!flag)
{
nInbufSize += sprintf(szInbuf+nInbufSize, "\n</JpegPictureWithAppendData>");
}
}
else
{
printf("value is null\n");
}
}
printf("\nszInbuf = %s \n",szInbuf);
return ;
}

如果是都xml文件的话只需要加上读写文件操作就可以使用了。

这个是非常简单的xml文件解析

其实还需要一个错误控制,对于遇到的每一个错误都有相应的返回码,在读取文件错误的时候可以准确的定位到错误在哪里发生的。

最新文章

  1. MS Sql Server
  2. ios如何在#import方面提升编译性能
  3. 多行图片hover加边框兼容IE7+
  4. 关于a标签点击会出现的背景色的问题
  5. SQL Server安全(1/11):SQL Server安全概述
  6. 排序图解:js排序算法实现
  7. Charles使用详情
  8. 第23章 SEH结构化异常处理(3)_终止处理程序
  9. Hark的数据结构与算法练习之耐心排序
  10. java 页面换行处理
  11. 实现CCLayer只显示一个矩形可见区域
  12. 缓冲运动-1-[解决1].html
  13. HTML+CSS笔记 CSS入门续集
  14. [Just a feeling]
  15. KMP算法的实现
  16. Illegal resource reference: @*android resources are private and not always present
  17. jenkins构建后操作添加“Publish to Subversion repository”与Eclipse更新提交SVN文件冲突
  18. Android Widget小组件开发(一)——Android实现时钟Widget组件的步骤开发,这些知识也是必不可少的!
  19. Atlantis HDU - 1542 (扫描线,线段树)
  20. python,关于用户登录与注册问题

热门文章

  1. 分布式 master/slave 框架
  2. python yield from (二)
  3. 微软宣布.NET Native预览版 C#可编译为本地机器码【转】
  4. MySQL5.6升级到5.7详细教程
  5. C#中使用WCF创建面向网络的服务程序
  6. Java编程基础——运算符和进制
  7. vue笔记(一)
  8. maven 学习---Maven外部依赖
  9. SpringData系列四 @Query注解及@Modifying注解@Query注解及@Modifying注解
  10. 5.如何基于 dubbo 进行服务治理、服务降级、失败重试以及超时重试?