Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > A c program which printing the tag value of a xml file using expat parser in linux environment

Reply
Thread Tools

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

 
 
sharan
Guest
Posts: n/a
 
      10-25-2007
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

 
Reply With Quote
 
 
 
 
Juergen Kahrs
Guest
Posts: n/a
 
      10-25-2007
sharan wrote:
> 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-sets

The script for XMLgawk might look like this:

@load xml
XMLCHARDATA { printf $0 }

 
Reply With Quote
 
 
 
 
Pavel Lepin
Guest
Posts: n/a
 
      10-25-2007

Juergen Kahrs <(E-Mail Removed)> wrote in
<(E-Mail Removed)>:
> 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 [i]) ;
}

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
 
Reply With Quote
 
sidsverma sidsverma is offline
Junior Member
Join Date: Aug 2011
Posts: 3
 
      08-24-2011
Quote:
Originally Posted by Pavel Lepin View Post
Juergen Kahrs <(E-Mail Removed)> wrote in
<(E-Mail Removed)>:
> 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 [i]) ;
}

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
 
Reply With Quote
 
sidsverma sidsverma is offline
Junior Member
Join Date: Aug 2011
Posts: 3
 
      08-26-2011
Quote:
Originally Posted by Pavel Lepin View Post
Juergen Kahrs <(E-Mail Removed)> wrote in
<(E-Mail Removed)>:
> 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?
 
Reply With Quote
 
sidsverma sidsverma is offline
Junior Member
Join Date: Aug 2011
Posts: 3
 
      08-26-2011
hey i got how do do it!!!
it was damn easy
just printf "name" in either the start or the end element handler!!!
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
build a hierarchical tree, without using DOM,schema, and sax using expat parser and c pharioum@gmail.com XML 2 11-05-2007 02:24 PM
using expat parser how to build a tree shaped data structure of a xml document and read the value from it in c language sharan XML 4 11-05-2007 08:59 AM
Want help on how we convert output to tabular format Using the expat parser (http://expat.sourceforge.net/) i have to parse the following xml file and print it on the screen in tabular format. sharan XML 1 10-26-2007 01:20 PM
Using the expat parser (http://expat.sourceforge.net/) i have to parse the following xml file and print it on the screen in tabular format. Want a c program on that! sharan XML 1 10-26-2007 07:56 AM
how do u invoke Tag b's Tag Handler from within Tag a's tag Handler? shruds Java 1 01-27-2006 03:00 AM



Advertisments