Home | History | Annotate | Download | only in examples
      1 /* This is simple demonstration of how to use expat. This program
      2    reads an XML document from standard input and writes a line with
      3    the name of each element to standard output indenting child
      4    elements by one tab stop more than their parent element.
      5    It must be used with Expat compiled for UTF-8 output.
      6                             __  __            _
      7                          ___\ \/ /_ __   __ _| |_
      8                         / _ \\  /| '_ \ / _` | __|
      9                        |  __//  \| |_) | (_| | |_
     10                         \___/_/\_\ .__/ \__,_|\__|
     11                                  |_| XML parser
     12 
     13    Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
     14    Copyright (c) 2000-2017 Expat development team
     15    Licensed under the MIT license:
     16 
     17    Permission is  hereby granted,  free of charge,  to any  person obtaining
     18    a  copy  of  this  software   and  associated  documentation  files  (the
     19    "Software"),  to  deal in  the  Software  without restriction,  including
     20    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
     21    distribute, sublicense, and/or sell copies of the Software, and to permit
     22    persons  to whom  the Software  is  furnished to  do so,  subject to  the
     23    following conditions:
     24 
     25    The above copyright  notice and this permission notice  shall be included
     26    in all copies or substantial portions of the Software.
     27 
     28    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
     29    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
     30    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
     31    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
     32    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
     33    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     34    USE OR OTHER DEALINGS IN THE SOFTWARE.
     35 */
     36 
     37 #include <stdio.h>
     38 #include <expat.h>
     39 
     40 #ifdef XML_LARGE_SIZE
     41 # if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400
     42 #  define XML_FMT_INT_MOD "I64"
     43 # else
     44 #  define XML_FMT_INT_MOD "ll"
     45 # endif
     46 #else
     47 # define XML_FMT_INT_MOD "l"
     48 #endif
     49 
     50 #ifdef XML_UNICODE_WCHAR_T
     51 # include <wchar.h>
     52 # define XML_FMT_STR "ls"
     53 #else
     54 # define XML_FMT_STR "s"
     55 #endif
     56 
     57 static void XMLCALL
     58 startElement(void *userData, const XML_Char *name, const XML_Char **atts)
     59 {
     60   int i;
     61   int *depthPtr = (int *)userData;
     62   (void)atts;
     63 
     64   for (i = 0; i < *depthPtr; i++)
     65     putchar('\t');
     66   printf("%" XML_FMT_STR "\n", name);
     67   *depthPtr += 1;
     68 }
     69 
     70 static void XMLCALL
     71 endElement(void *userData, const XML_Char *name)
     72 {
     73   int *depthPtr = (int *)userData;
     74   (void)name;
     75 
     76   *depthPtr -= 1;
     77 }
     78 
     79 int
     80 main(int argc, char *argv[])
     81 {
     82   char buf[BUFSIZ];
     83   XML_Parser parser = XML_ParserCreate(NULL);
     84   int done;
     85   int depth = 0;
     86   (void)argc;
     87   (void)argv;
     88 
     89   XML_SetUserData(parser, &depth);
     90   XML_SetElementHandler(parser, startElement, endElement);
     91   do {
     92     size_t len = fread(buf, 1, sizeof(buf), stdin);
     93     done = len < sizeof(buf);
     94     if (XML_Parse(parser, buf, (int)len, done) == XML_STATUS_ERROR) {
     95       fprintf(stderr,
     96               "%" XML_FMT_STR " at line %" XML_FMT_INT_MOD "u\n",
     97               XML_ErrorString(XML_GetErrorCode(parser)),
     98               XML_GetCurrentLineNumber(parser));
     99       return 1;
    100     }
    101   } while (!done);
    102   XML_ParserFree(parser);
    103   return 0;
    104 }
    105