Thursday, April 7, 2011

xml file tokenizing in c coding

//header files
# include "stdio.h"
# include "string.h"
# include "stdlib.h"

//FunctionDeclaration
int YourFunction(char**,char*,char*);

//Global variable
char *cPagexml = "<root><device><producer>Indian politician</producer><problem>barrier for india's growth </problem></device></root>";

int main()
{
//Variable Declaration
char *TagName = "producer";
char *xml = NULL;
char *Tagvalue = "public must react about it";

//Memory allocation
xml = (char *)calloc(1024,1);

//To copy the xml file
strcpy(xml,cPagexml);

printf("Fist Time : %s\n\n\n",xml);

//Call function
YourFunction(&xml,TagName,Tagvalue);


printf("After your function : %s\n\n\n\n",xml);

return 0;

}

/****************************** function definition ****************************************
User want to write this function to get the output


***********************************************************************************************/

int YourFunction(char**xml,char*TagName,char*TagValue)
{
//Variable declartion
int nCount,nLoop;
int nTagValueLength;
int nLength;
char *cTempdata = (char *)calloc(1024,1);
char *cTemp = (char *)calloc(1024,1);
char *tempvalue=NULL;


// To move the pointer to producer
cTemp = strstr(*xml, TagName);

//To count the number of character pointer moved
nCount = (int)( cTemp - *xml);

// Cut the first ncount char from source
strncpy(cTempdata,*xml,nCount);

//to assign to other variable
nLoop = nCount;
// To reach the crrent position as tag
while(nLoop != 0)
{
nLoop--;
*cTempdata++;
}


// to reach the end of the tag name
while (*cTemp != '>')
{
// to assign in new pointer
*cTempdata++ = *cTemp++;
nCount++;
}
//for > symbol
*cTempdata++ = *cTemp++;
nCount++;

tempvalue = strstr(cTemp,TagName);
nTagValueLength = strlen(TagValue);
nLength = tempvalue - cTemp;
nLength -=2; //for </ symbols of end tag

if(nTagValueLength <= nLength)
{

while (*TagValue != '\0')
{
*cTempdata++ = *TagValue++;
*cTemp++;
++nCount;
}

nLength = (nLength-nTagValueLength);

while (nLength !=0)
{
nLength--;
*cTemp++;
}
}
else
{
while (*TagValue != '\0')
{
*cTempdata++ = *TagValue++;

++nCount;
}
while(nLength-- != 0)
*cTemp++;



}



while (*cTemp != '\0')
{

*cTempdata++ = *cTemp++;
nCount++;
}



// for reassigning the contents

nLoop = nCount;
while (nLoop--!= 0)
{
cTempdata--;
}

// To reassign the value to xml pointer
strcpy(*xml,cTempdata);


return 0;
}




OUTPUT:

Fist Time : <root><device><producer>Indian politician</producer><problem>barrier for india's growth </problem></device></root>


After your function : <root><device><producer>public must react about it</producer><problem>barrier for india's growth </problem></device></root>



Press any key to continue . . .





Indian Economy: Performance and Policies


No comments:

Post a Comment