[Expat-discuss] extracting value other than element and attribute
yuki latt
yuki.latt at gmail.com
Tue Aug 5 03:32:32 CEST 2008
Hello,
Thank you so much for your kind help. I am sorry to bother you again and
again.
Although I have tried it in several ways, I havent get it yet.
My simple xml file is as follows. (real xml file is sooo huge)
<Company ID="001">
<Department>
<Administration>
<Head> Mr. Smith </Head>
<Staff> Mr. Clark</Staff>
</Administration>
</Department>
</Company>
I want to extract the names of the heads of all departments ( Mr. Smith).
Followings are my dirty codes. :)
-----------------------------------------------------------------------------------
void XMLCALL elementText(void *userData,const char *xmlData,int len)
{
char* startTag = "<Head>";
char* endTag = "</Head>";
char* startLocation;
char* endLocation;
char* tempString=NULL;
int tempLength;
while(strstr(xmlData,startTag)!=NULL)
{
// find the length of string of " <Head> ... </Company>
startLocation = strstr(xmlData,startTag)+strlen(startTag);
// find the length of string of " </Head> ... </Company>
endLocation = strstr(xmlData,endTag); //Find the end of your
string
// find the difference
tempLength = endLocation - startLocation; //Calculate the length
of the string
// allocate the memory for tempString
tempString = malloc(tempLength);
// Copy string to tempString ( it will be Mr. Smith)
strncpy(tempString,startLocation,tempLength); //Extract your
string
printf("%s \n",tempString);
free(tempString);
break;
}
}
If there is only one entry for <Head> </Head> as above, it works.
But the program still prints Mr. Smith for 4 times. I dont know how to
handle it. :(
If there is two entries for Head as follows
<Administration>
<Head> Mr. Smith </Head>
<Staff> Mr. Clark</Staff>
</Administration>
<Account>
<Head>Mr. James</Head>
<Staff>Mr. Paul</Staff>
</Account> )
the program shows the following erros-
12 [main] my_exe 416 _cygtls::handle_exceptions: Error while dumping state
(probably corrupted stack)
Segmentation fault (core dumped)
Any help would be much appreciated.
regards,
yuki
More information about the Expat-discuss
mailing list