A c program which printing the tag value of a xml file using expat parser in linux environment

S

sharan

like for a example xml program
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

out put should be :
Tove
Jani
Reminder
Don't forget me this weekend!
i want the c program for that in linux environments
 
J

Juergen Kahrs

sharan said:
like for a example xml program
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

out put should be :
Tove
Jani
Reminder
Don't forget me this weekend!
i want the c program for that in linux environments

If you need a C application, take Expat and write your
own application around it. Should be about 200 lines.

http://expat.sourceforge.net/

If you are free to use a scripting language, you might try this one:

http://home.vrweb.de/~juergen.kahrs...Character-data-and-encoding-of-character-sets

The script for XMLgawk might look like this:

@load xml
XMLCHARDATA { printf $0 }
 
P

Pavel Lepin

Juergen Kahrs said:
If you need a C application, take Expat and write your
own application around it. Should be about 200 lines.

About 80, and that's with a good bit of perverse logic
thrown in:

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

const char * chk_str (const char * str) {
for (; * str ; ++ str)
switch (* str) {
case ' ' : case '\t' : case '\r' : case '\n' :
break ;
default : return str ;
}
return str ;
}

#define C_BUF_CHUNK 8

char * c_buf = NULL ;
size_t c_buf_size = 0 ;
size_t c_buf_ptr = 0 ;

void c_buf_init () {
assert (c_buf = malloc (c_buf_size = C_BUF_CHUNK)) ;
c_buf [0] = '\n' ; c_buf [1] = '\0' ;
}

void c_buf_gulp () {
assert (
c_buf = realloc (c_buf , c_buf_size += C_BUF_CHUNK)
) ;
}

void c_buf_free () {
free (c_buf) ;
}

void c_buf_push (char c) {
if (c_buf_ptr == c_buf_size) c_buf_gulp () ;
c_buf [c_buf_ptr ++] = c ;
}

const char * c_buf_get () {
c_buf_push ('\0') ; c_buf_ptr = 0 ;
return c_buf ;
}

void c_buf_flush () {
const char * str = c_buf_get () ;
printf ("%s" , chk_str (str)) ;
if (strlen (str) && '\n' != str [strlen (str) - 1])
printf ("\n") ;
}

void elt_st (
void * usr_d ,
const XML_Char * name , const XML_Char ** attr
) {
c_buf_flush () ;
}

void elt_end (void * usr_d , const XML_Char * name) {
c_buf_flush () ;
}

void node_txt
(void * usr_d , const XML_Char * txt , int len) {
int i ;
for (i = 0 ; i != len ; ++ i) c_buf_push (txt ) ;
}

int main () {
char buf ;
XML_Parser prsr = XML_ParserCreate (NULL) ;
c_buf_init () ;
XML_SetElementHandler (prsr , &elt_st , &elt_end) ;
XML_SetCharacterDataHandler (prsr , &node_txt) ;
while (EOF != (buf = getchar ()))
XML_Parse (prsr , &buf , 1 , 0) ;
c_buf_free () ;
return 0;
}

Simply dumping text node content would likely fit in one
screen.

Note that I do not endorse the development process and
coding style (or lack thereof) prominently displayed in
this code snippet.
 
Joined
Aug 24, 2011
Messages
3
Reaction score
0
Pls explain!

Juergen Kahrs <[email protected]> wrote in
<[email protected]>:
> sharan wrote:
>> <?xml version="1.0" encoding="ISO-8859-1"?>
>> <note>
>> <to>Tove</to>
>> <from>Jani</from>
>> <heading>Reminder</heading>
>> <body>Don't forget me this weekend!</body>
>> </note>
>>
>> i want the c program for that in linux environments

>
> If you need a C application, take Expat and write your
> own application around it. Should be about 200 lines.


About 80, and that's with a good bit of perverse logic
thrown in:

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

const char * chk_str (const char * str) {
for (; * str ; ++ str)
switch (* str) {
case ' ' : case '\t' : case '\r' : case '\n' :
break ;
default : return str ;
}
return str ;
}

#define C_BUF_CHUNK 8

char * c_buf = NULL ;
size_t c_buf_size = 0 ;
size_t c_buf_ptr = 0 ;

void c_buf_init () {
assert (c_buf = malloc (c_buf_size = C_BUF_CHUNK)) ;
c_buf [0] = '\n' ; c_buf [1] = '\0' ;
}

void c_buf_gulp () {
assert (
c_buf = realloc (c_buf , c_buf_size += C_BUF_CHUNK)
) ;
}

void c_buf_free () {
free (c_buf) ;
}

void c_buf_push (char c) {
if (c_buf_ptr == c_buf_size) c_buf_gulp () ;
c_buf [c_buf_ptr ++] = c ;
}

const char * c_buf_get () {
c_buf_push ('\0') ; c_buf_ptr = 0 ;
return c_buf ;
}

void c_buf_flush () {
const char * str = c_buf_get () ;
printf ("%s" , chk_str (str)) ;
if (strlen (str) && '\n' != str [strlen (str) - 1])
printf ("\n") ;
}

void elt_st (
void * usr_d ,
const XML_Char * name , const XML_Char ** attr
) {
c_buf_flush () ;
}

void elt_end (void * usr_d , const XML_Char * name) {
c_buf_flush () ;
}

void node_txt
(void * usr_d , const XML_Char * txt , int len) {
int i ;
for (i = 0 ; i != len ; ++ i) c_buf_push (txt ) ;
}

int main () {
char buf ;
XML_Parser prsr = XML_ParserCreate (NULL) ;
c_buf_init () ;
XML_SetElementHandler (prsr , &elt_st , &elt_end) ;
XML_SetCharacterDataHandler (prsr , &node_txt) ;
while (EOF != (buf = getchar ()))
XML_Parse (prsr , &buf , 1 , 0) ;
c_buf_free () ;
return 0;
}

Simply dumping text node content would likely fit in one
screen.

Note that I do not endorse the development process and
coding style (or lack thereof) prominently displayed in
this code snippet.

--
It is rare to find learned men who are clean, do not stink,
and have a sense of humour. -- Liselotte in a letter to
Sophie, 30 Jul 1705




Can u pls explain how it really works? especially the element handlers...
thanks :)
 
Joined
Aug 24, 2011
Messages
3
Reaction score
0
Juergen Kahrs <[email protected]> wrote in
<[email protected]>:
> sharan wrote:
>> <?xml version="1.0" encoding="ISO-8859-1"?>
>> <note>
>> <to>Tove</to>
>> <from>Jani</from>
>> <heading>Reminder</heading>
>> <body>Don't forget me this weekend!</body>
>> </note>
>>
>> i want the c program for that in linux environments

>
> If you need a C application, take Expat and write your
> own application around it. Should be about 200 lines.



hey i want to capture the tag names as well...
which means i want to display the result as:

note:
\t to: Tove
\t from: Jani
\t heading: Reminder
\t body: Don't forget me this weekend!



how do i do this?
 
Joined
Aug 24, 2011
Messages
3
Reaction score
0
hey i got how do do it!!!
it was damn easy :)
just printf "name" in either the start or the end element handler!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top