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