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

Discussion in 'XML' started by sharan, Oct 25, 2007.

  1. sharan

    sharan Guest

    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
     
    sharan, Oct 25, 2007
    #1
    1. Advertising

  2. Re: A c program which printing the tag value of a xml file usingexpat parser in linux environment

    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-data-and-encoding-of-character-sets

    The script for XMLgawk might look like this:

    @load xml
    XMLCHARDATA { printf $0 }
     
    Juergen Kahrs, Oct 25, 2007
    #2
    1. Advertising

  3. sharan

    Pavel Lepin Guest

    Juergen Kahrs <> wrote in
    <>:
    > 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
     
    Pavel Lepin, Oct 25, 2007
    #3
  4. sharan

    sidsverma

    Joined:
    Aug 24, 2011
    Messages:
    3
    Pls explain!





    Can u pls explain how it really works? especially the element handlers...
    thanks :)
     
    sidsverma, Aug 24, 2011
    #4
  5. sharan

    sidsverma

    Joined:
    Aug 24, 2011
    Messages:
    3


    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?
     
    sidsverma, Aug 26, 2011
    #5
  6. sharan

    sidsverma

    Joined:
    Aug 24, 2011
    Messages:
    3
    hey i got how do do it!!!
    it was damn easy :)
    just printf "name" in either the start or the end element handler!!!
     
    sidsverma, Aug 26, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shruds
    Replies:
    1
    Views:
    871
    John C. Bollinger
    Jan 27, 2006
  2. sharan
    Replies:
    1
    Views:
    727
    Pavel Lepin
    Oct 26, 2007
  3. sharan
    Replies:
    4
    Views:
    1,293
    Pavel Lepin
    Nov 5, 2007
  4. Replies:
    2
    Views:
    947
    Joseph Kesselman
    Nov 5, 2007
  5. Tobias Blass

    expat XML parser manpage

    Tobias Blass, Dec 11, 2010, in forum: XML
    Replies:
    2
    Views:
    1,285
    Tobias Blass
    Dec 11, 2010
Loading...

Share This Page