LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   extracting data from xml file using c in linux (https://www.linuxquestions.org/questions/linux-newbie-8/extracting-data-from-xml-file-using-c-in-linux-788387/)

karthikmca 02-11-2010 01:47 AM

extracting data from xml file using c in linux
 
hi,
I have to extract data from xml file through c coding in linux.
I am using expat library function in my coding.

whats my problem is ........


xml_set_element_handler(parser,start,end);

here start and end are call back function. they are not invoked?

please help me!

Aquarius_Girl 02-11-2010 02:55 AM

Well, it would be better if you could show the code with which u are attempting the same !

karthikmca 02-11-2010 05:31 AM

hi anisha,

this is my code i am using. this code gets compiled with error free. but the call back function start and end are not invoking by the parser!!!.
Code:

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

void XMLCALL start(void *userData,const XML_Char *name,const XML_Char **atts);
void XMLCALL end(void *userData,const XML_Char *name);
void XMLCALL chard(void *ud,const XML_Char *s,int len);

int main()
{
XML_Parser p;
p=XML_ParserCreate("US-ASCII");

char *str,*str1;
str="<name att='2323' att2='545'>test value</name>";
str1="<name>";

int x=0;
x=strlen(str);

printf("%d",XML_Parse(p,str,x,1));

XML_SetUserData(p,str);
XML_SetElementHandler(p,start,end);
return 1;
}
void XMLCALL start(void *userData, const XML_Char *name,const XML_Char **atts)
{
printf("%s",name);
}
void XMLCALL end(void *userData,const XML_Char *name)
{
printf("End");
}

Aquarius_Girl 02-15-2010 03:33 AM

Quote:

whats my problem is ........


xml_set_element_handler(parser,start,end);

here start and end are call back function. they are not invoked?
Where is this xml_set_element_handler(parser,start,end); being defined and called from in the code u have shown ?

karthikmca 02-15-2010 06:18 AM

hi anisha,
I modified the coding ,and all the call back functions were get invoked, but i dont know how to assign value to len argument in the chard function.

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

void XMLCALL start(void *str,const XML_Char *name,const XML_Char **atts);
void XMLCALL end(void *userData,const XML_Char *name);
void XMLCALL chard(void *ud,const XML_Char *s,int len);

int main()
{
XML_Parser p;
p=XML_ParserCreate("US-ASCII");

char *str;
str="<name1 att='2323' att2='545'> hi every onle<t1 id='123' iq='234'> test value1 </t1> <t2 ix='333'>test value2</t2></name1>";

int x=0;
x=strlen(str);


XML_SetElementHandler(p,start,end);
XML_SetCharacterDataHandler(p,chard);
printf("parser status:%d\n",XML_Parse(p,str,x,1));
XML_SetUserData(p,str);

return 1;
}
void XMLCALL start(void *str, const XML_Char *name,const XML_Char **atts)
{
int i;
for(i=0;atts[i];i+=2)
{
printf("\n%s='%s'\n",atts[i],atts[i+1]);
}
}
void XMLCALL end(void *userData,const XML_Char *name)
{
printf("End\n");
}
void XMLCALL chard(void *ud,const XML_Char *s,int len)
{
printf("\n chard:%s\n",s);
}

"new2.c" 52L, 1249C

Aquarius_Girl 02-15-2010 06:32 AM

Quote:

I modified the coding ,and all the call back functions were get invoked, but i dont know how to assign value to len argument in the chard function.
How are u assigning values to other arguements of "chard" function ?
Besides u have not mentioned where and how this "chard" function is getting called ?

karthikmca 02-16-2010 12:24 AM

chard is a call back function.

XML_SetCharacterDataHandler(p,chard); thru this function chard is invoked.

the arguments are automatically get assigned


All times are GMT -5. The time now is 10:40 PM.