XML request in server communication

Create an XML Request in “c” for server communication.

We use XML to carry data between server and client. XML simplifies the data transportation and carry data in plain text format which is readable by both human and machine. It is the biggest reason to provide a software and hardware independent way of transporting, storing, and sharing data. This feature of XML gives the reason to send the request in XML format in server communication.

In case of POS (point of sale), we generally send the Request in XML, JSON or ISO bitmap format to communicate with payment gateway or middle-ware server. A lot of API available in “C” to create XML or bitmap request for server communication.
In this article, I will describe a simple project to create a XML request for server communication.

Let’s talk an  example

Consider a scenario where we want to send some user credential and a message to the server in XML format. In this situation first, we need to create an XML tag and feel these tag with desired data.
Note:  XML language has no predefined tag. It provides flexibility to give any name to your XML tag.

 

If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.

Your free trial is waiting

 

Below API create an XML tag and sets the desired data. If there is no source data then tag automatically fill by “0”.You can modify these API as per your requirements. When finally tag has created then simply copy these tags to the request buffer.

API To Create the XML Tag

/*Function to create a XML Tag*/
void SetValueInXmlTag(char *pszXmlRequest,const char *pszSource,const char *pszXmlTag)
{

    short iLen=0;
    char acXmlTag[60]= {0};


    iLen =strlen(pszSource);

    if(0 == iLen)  //If source buffer contain no data.
    {
        //Create XML Tag
        sprintf(acXmlTag,"<%s>%s</%s>",pszXmlTag,"0",pszXmlTag);
    }
    else
    {
        //Create XML Tag
        sprintf(acXmlTag,"<%s>%s</%s>",pszXmlTag,pszSource,pszXmlTag);
    }

    strcat(pszXmlRequest,acXmlTag); //Copy XML tag to the request buffer.

}

 

Program to create an XML request for server communication

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

/**Structure, contains a field which will be sent to the server**/
typedef struct
{
    char aszMessage[64];
    char aszUserId[20];
    char aszPassword[12];
} sRequestData;



// Function to create a XML Tag
void SetValueInXmlTag(char *pszXmlRequest,const char *pszSource,const char *pszXmlTag)
{

    short iLen=0;
    char acXmlTag[120]= {0};


    iLen =strlen(pszSource);

    if(0 == iLen)
    {
        sprintf(acXmlTag,"<%s>%s</%s>",pszXmlTag,"0",pszXmlTag);
    }
    else
    {
        sprintf(acXmlTag,"<%s>%s</%s>",pszXmlTag,pszSource,pszXmlTag);
    }
    strcat(pszXmlRequest,acXmlTag);
}



//XML Request which will be send to the server.
short XmlRequest(sRequestData *psRequestData,char *pszXmlRequest)
{
    short lRequest=0;

    /*Create XML Request*/
    strcpy(pszXmlRequest,"<BODY>");
    SetValueInXmlTag(pszXmlRequest,psRequestData->aszUserId,"USER_ID");
    SetValueInXmlTag(pszXmlRequest,psRequestData->aszPassword,"PASSWORD");
    SetValueInXmlTag(pszXmlRequest,psRequestData->aszMessage,"MESSAGE");
    strcat(pszXmlRequest,"</BODY>");
    lRequest=strlen(pszXmlRequest);

    return lRequest; // Length of request data
}


// Driver program
int main()
{
    sRequestData RequestData = {0}; //structure to contain data
    char aszXmlRequest[200]= {0}; //Buffer to store XML request data

    printf("Enter User Id = ");
    scanf("%s",&RequestData.aszUserId);

    printf("\n\nEnter Password = ");
    scanf("%s",&RequestData.aszPassword);

    printf("\n\nEnter Message = ");
    scanf("%s",&RequestData.aszMessage);

    XmlRequest(&RequestData,aszXmlRequest);
    printf("\n\nRequest = %s\n\n\n\n\n\n",aszXmlRequest);

    return 0;
}

 

OutPut:

server communication

 

Recommended Post