LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-21-2007, 07:36 AM   #1
gopi_raghu
LQ Newbie
 
Registered: Aug 2006
Posts: 11

Rep: Reputation: 0
XML file parser using C language


I am new to parsing XML file using C

I need to parse the XML file and get the values between tags(start and end).I am using expat library(expat.h) and am having C API as fallows

XML_SetElementHandler(parser, startElement, endElement);

static void XMLCALL
startElement(void *userData, const char *name, const char **atts)
{
int i;
int natts;
int *depthPtr = (int *)userData;
const XML_Char **p;

for (i = 0; i < global_depth; i++)
printf(" ");

printf("%s\n", name);
LINE:8 for (i = 0; atts[i]; i += 2)
printf("%s = %s", atts[i], atts[i + 1]);

printf("\n");
// printf("%d\n", global_depth);
global_depth++;

// printf("%s = %s\n", atts[0], atts[1]);
// puts(name);
// *depthPtr += 1;
}

I am trying to print the values between start and end tag but is not printing (LINE no 8 in code).

Any code to get values between start and end tag of XML file ?

Thanks in advance
 
Old 08-21-2007, 11:11 AM   #2
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
The atts argument passed in is the element attributes. So, if you have something like this:
Code:
<some-element id="blah" name="Joe Blow">
Some text.
</some-element>
The atts will hold something like this:
Code:
atts[0] = "id"
atts[1] = "blah"
atts[2] = "name"
atts[3] = "Joe Blow"
To get "Some text.", you have to set up a CharacterData (NOT Cdata) handler. This will get the text passed into it. A warning though, most XML parsers, expat included, may split up the text. You can't count on having the entire string "Some text." given to you on a single call of your CharacterData handler, it may be split up across 2 or more. So, you would have to have some function like:

Code:
/* ... */
        XML_SetCharacterDataHandler(parser, text_handler);
/* ... */
void text_handler(void * user_data, const XML_Char *s, int len)
{
        fwrite(s, 1, len, stdout);

        /* this may be put in the middle of the text */
        putchar('\n'); 

        /* perhaps something to keep track of all the text */
        /* buffer_append(((buffer *) user_data), s, len); */
}
With expat, it REALLY pays off to read the header file (expat.h).

Edit: Note, the, XML_Char *, text passed in is not NULL terminated.

Last edited by 95se; 08-21-2007 at 11:15 AM.
 
Old 08-22-2007, 01:06 AM   #3
gopi_raghu
LQ Newbie
 
Registered: Aug 2006
Posts: 11

Original Poster
Rep: Reputation: 0
Thanks

Thanks a lot.
Got the output.

bye

T.Raghavendra










Quote:
Originally Posted by 95se View Post
The atts argument passed in is the element attributes. So, if you have something like this:
Code:
<some-element id="blah" name="Joe Blow">
Some text.
</some-element>
The atts will hold something like this:
Code:
atts[0] = "id"
atts[1] = "blah"
atts[2] = "name"
atts[3] = "Joe Blow"
To get "Some text.", you have to set up a CharacterData (NOT Cdata) handler. This will get the text passed into it. A warning though, most XML parsers, expat included, may split up the text. You can't count on having the entire string "Some text." given to you on a single call of your CharacterData handler, it may be split up across 2 or more. So, you would have to have some function like:

Code:
/* ... */
        XML_SetCharacterDataHandler(parser, text_handler);
/* ... */
void text_handler(void * user_data, const XML_Char *s, int len)
{
        fwrite(s, 1, len, stdout);

        /* this may be put in the middle of the text */
        putchar('\n'); 

        /* perhaps something to keep track of all the text */
        /* buffer_append(((buffer *) user_data), s, len); */
}
With expat, it REALLY pays off to read the header file (expat.h).

Edit: Note, the, XML_Char *, text passed in is not NULL terminated.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
checking for XML::Parser... configure: error: XML::Parser perl module is required for kornerr Linux - General 11 11-16-2008 07:24 AM
XML parser using C language gopi_raghu Programming 6 08-21-2007 12:47 PM
XML Parser... Prada Linux - Software 2 04-17-2007 11:26 AM
XML Parser bulkman Linux - Software 6 04-29-2005 07:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration