Parse XML response in C without using library

Parse XML response in C without using library

An XML is the easiest way to carry the data in server communication. It carries data in plain text format which is readable by both humans and machines. There are many libraries available to parse XML responses in C/C++ for example Expat and libxml. But sometimes we need to create our own function to parse XML responses in C without using the library.

You can also see, how to create an XML request in C without using a library.

In this article, I will show you how we can parse XML response in C without using the library. You can also modify the XML parsing function as per your requirement. So let’s see the code.

#include <stdio.h>
#include <string.h>

int GetXmlTagValue(char *pResBuf, char *pTag, char *pTagValue)
{
    int len=0, pos = 0;
    char openTag[100] = {0}; //Opening Tag
    char closeTag[100] = {0};//Closing Tag
    int posOpenTag=0, posClosingTag=0;
    //check enter buffer
    len = strlen(pResBuf);
    if (len<=0)
    {
        return -1;
    }

    //Create Opening Tag
    memset(openTag, 0, sizeof(openTag));
    strcpy(openTag, "<");
    strcat(openTag, pTag);
    strcat(openTag, ">");

    //Create Closing tag
    memset(closeTag, 0, sizeof(closeTag));
    strcpy(closeTag, "</");
    strcat(closeTag, pTag);
    strcat(closeTag, ">");

    //Get len of open and close tag
    const int lenOpenTag = strlen(openTag);
    const int lenCloseTag = strlen(closeTag);

    //Get Opening tag position
    for (pos=0; pos<len; pos++)
    {
        if ( !memcmp(openTag,(pResBuf+pos),lenOpenTag))
        {
            posOpenTag = pos;
            break;
        }
    }

    //Get closing tag position
    for (pos=0; pos<len; pos++)
    {
        if ( !memcmp(closeTag,(pResBuf+pos),lenCloseTag) )
        {
            posClosingTag = pos;
            break;
        }
    }

    //get the tag value
    if ( (posOpenTag !=0) && (posClosingTag !=0) )
    {
        const int lenTagVal = posClosingTag-posOpenTag-lenOpenTag;
        const char * const pStartPosTagVal = pResBuf+posOpenTag+lenOpenTag;
        if (lenTagVal)
        {
            //Get tag value
            memcpy(pTagValue,pStartPosTagVal, lenTagVal);
            if (strlen(pTagValue))
            {
                return 1;
            }
        }
    }
    return -1;
}


int main()
{
    //Response message
    char Response[]="<Response>\
    <Name>aticleworld.com</Name>\
    <year>1.5</year>\
    <BlogType>Embedded C and C++</BlogType>\
    <Author>amlendra</Author>\
    </Response>";

    //pTag name which value you want to access
    char pTag[] = "BlogType";

    //Buffer to store tag value
    char pTagValue[100]= {0};

    //Function to get tag value
    GetXmlTagValue(Response,pTag,pTagValue);

    //Print pTag Value
    printf("%s",pTagValue);

    return 0;
}

Output: Embedded C  and C++

 

How is the parsing XML function work?

 

1. First, need to create an open and closing tag.

//Create Opening Tag
memset(openTag, 0, sizeof(openTag));
strcpy(openTag, "<");
strcat(openTag, pTag);
strcat(openTag, ">");

//Create Closing tag
memset(closeTag, 0, sizeof(closeTag));
strcpy(closeTag, "</");
strcat(closeTag, pTag);
strcat(closeTag, ">");

 

2.  After that find the position of the tags in response.

//Get Opening tag position
for (pos=0; pos<len; pos++)
{
    if ( !memcmp(openTag,pResBuf+pos,lenOpenTag))
    {
        posOpenTag = pos;
        break;
    }
}


//Get closing tag position
for (pos=0; pos<len; pos++)
{
    if ( !memcmp(closeTag,pResBuf+pos,lenCloseTag) )
    {
        posClosingTag = pos;
        break;
    }
}

 

3. If the tag is present in the response then parse the tag value from the response using the arithmetic operation.

if ( (posOpenTag !=0) && (posClosingTag !=0) )
{
    const int lenTagVal = posClosingTag-posOpenTag-lenOpenTag;
    char *pStartPosTagVal = pResBuf+posOpenTag+lenOpenTag;
    if (lenTagVal)
    {
        //Get tag value
        memcpy(pTagValue,pStartPosTagVal, lenTagVal);
        if (strlen(pTagValue))
        {
            return 1;
        }
    }
}

 

Your opinion matters

Although here, I have tried to discuss Parse XML response in C without using library. But I would like to know your opinion on the XML parsing function. So please don’t forget to write a comment in the comment box.

 

Recommended Post for you