C实现简单的xml格式文件

Posted wanghao-boke

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C实现简单的xml格式文件相关的知识,希望对你有一定的参考价值。

今天在工作中遇到了一个需要处理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 = -1;
    if (pTmpXml == NULL || pValue == NULL || pKey == NULL)
    {
        return iRet;
    }

    memset(pValue, 0, sizeof(pValue));
    char BegKey[64] = {0};
    char EndKey[64] = {0};
    sprintf(BegKey, "<%s>", pKey);
    sprintf(EndKey, "</%s>", pKey);

    //printf("BegKey = %s
", BegKey);
    //printf("EndKey = %s
", EndKey);

    char *pBegin = pTmpXml;
    char *pEnd = NULL;

    int BegKey_len = strlen(BegKey);

    if ( (pBegin = strstr(pTmpXml, BegKey)) != NULL)
    {

        pEnd = strstr(pBegin + 1,EndKey );
        memcpy(pValue, pBegin + BegKey_len, pEnd - pBegin - BegKey_len);
        //printf("%s = %s
", BegKey, pValue);
        pTmpXml = pEnd + strlen(EndKey);
        //printf("pTmpXml = %s
",pTmpXml);
        iRet = 0;
    }
    else
    {
        iRet = -2;
    }
    
    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 = -1;
    if (pStrXml == NULL || pStuXml == NULL)
    {
        return iRet;  //
    }
    
    char szInbuf[1024] = {0};
    int nInbufSize = 0;
    nInbufSize += sprintf(szInbuf, "<?xml version="1.0" encoding="UTF-8"?>");

    char key[64] = {0};
    char value[64] = {0};
    char *pTmpXml = pStrXml;

    char KeyXml[][64] = {"id", "maxFrameRate", "reflectiveEnable", "reflectiveTemperature",
                          "emissivity", "distance", "refreshInterval", "distanceUnit",
                           "temperatureDataLength", "jpegPicEnabled"};
    int len_KeyXml = sizeof(KeyXml) / sizeof(KeyXml[0]);
    printf("len_KeyXml = %d
", len_KeyXml);
    for (int i = 0 ; i < len_KeyXml; i++)
    {
        //printf("KeyXml[%d] = %s
", i, KeyXml[i]);
        sprintf(key, "%s", KeyXml[i]);
        iRet = AnalysisXml(pTmpXml, value, key);

        if (value != NULL)
        {
            printf("%s = %s
", key, value);
            bool flag = (strcmp(key, "jpegPicEnabled"));
            if(!flag)
            {
                nInbufSize += sprintf(szInbuf+nInbufSize, "
<JpegPictureWithAppendData>");
            }
            nInbufSize += sprintf(szInbuf+nInbufSize, "
<%s>%s</%s>", key, value, key);
            if(!flag)
            {
                nInbufSize += sprintf(szInbuf+nInbufSize, "
</JpegPictureWithAppendData>");
            }
        }
        else
        {
            printf("value is null
");
        }   
    }

    printf("
szInbuf = %s 
",szInbuf);
    return 0;
}


int main()
{
    int iRet = -1;
    char Outxml[1024] = " <?xml version="1.0" encoding="UTF-8"?> 

<PixelToPixelParam version="2.0" xmlns="http://www.hikvision.com/ver20/XMLSchema"> 

<id>2</id>  
<maxFrameRate>400</maxFrameRate>    
<reflectiveEnable>false</reflectiveEnable>  
<reflectiveTemperature>20.00</reflectiveTemperature>
<emissivity>0.96</emissivity>
<distance>3000</distance>
<refreshInterval>50</refreshInterval>
<distanceUnit>centimeter</distanceUnit>
<temperatureDataLength>4</temperatureDataLength>
<JpegPictureWithAppendData>
<jpegPicEnabled>true</jpegPicEnabled>
</JpegPictureWithAppendData>
</PixelToPixelParam>";


    char Inxml[1024] = {0};

    float fEmissvity = 0.01;
    int wDistance = 10;
    //iRet = HandleXml(Outxml, &stuXml);

    char szInbuf[1024] = {0};
    int nInbufSize = 0;
    nInbufSize += sprintf(szInbuf, "<?xml version="1.0" encoding="UTF-8"?>");

    char key[64] = {0};
    char value[64] = {0};
    char *pTmpXml = Outxml;

    char KeyXml[][64] = {"id", "maxFrameRate", "reflectiveEnable", "reflectiveTemperature",
                          "emissivity", "distance", "refreshInterval", "distanceUnit",
                           "temperatureDataLength", "jpegPicEnabled"};
    int len_KeyXml = sizeof(KeyXml) / sizeof(KeyXml[0]);
    printf("len_KeyXml = %d
", len_KeyXml);
    for (int i = 0 ; i < len_KeyXml; i++)
    {
        //printf("KeyXml[%d] = %s
", i, KeyXml[i]);
        sprintf(key, "%s", KeyXml[i]);
        iRet = AnalysisXml(pTmpXml, value, key);

        if (value != NULL)
        {
            printf("%s = %s
", key, value);
            bool flag = (strcmp(key, "jpegPicEnabled"));
            if(!flag)
            {
                nInbufSize += sprintf(szInbuf+nInbufSize, "
<JpegPictureWithAppendData>");
            }

            if ( 0 == (strcmp(key, "emissivity")))
            {
                nInbufSize += sprintf(szInbuf+nInbufSize, "
<%s>%.2f</%s>", key, fEmissvity, key);
            }
            else if ( 0 == (strcmp(key, "distance")))
            {
                nInbufSize += sprintf(szInbuf+nInbufSize, "
<%s>%d</%s>", key, wDistance, key);
            }
            else
            {
                nInbufSize += sprintf(szInbuf+nInbufSize, "
<%s>%s</%s>", key, value, key);
            }

            if(!flag)
            {
                nInbufSize += sprintf(szInbuf+nInbufSize, "
</JpegPictureWithAppendData>");
            }
        }
        else
        {
            printf("value is null
");
        }   
    }
    printf("
szInbuf = %s 
",szInbuf);
    return 0;
}

 

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

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

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

以上是关于C实现简单的xml格式文件的主要内容,如果未能解决你的问题,请参考以下文章

需要示例代码片段帮助

在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途

Android片段XML布局问题

如何实现C++类与XML文件的转换

在 sapui5 片段的 xml 文件中使用 jquery

C#VS快捷键