testmxml.c 2.97 KB
Newer Older
Michael R Sweet's avatar
Michael R Sweet committed
1
/*
Michael R Sweet's avatar
Michael R Sweet committed
2
 * "$Id: testmxml.c,v 1.4 2003/06/04 01:23:21 mike Exp $"
Michael R Sweet's avatar
Michael R Sweet committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 *
 * Test program for mini-XML, a small XML-like file parsing library.
 *
 * Copyright 2003 by Michael Sweet.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Contents:
 *
 *   main()    - Main entry for test program.
 *   type_cb() - XML data type callback for mxmlLoadFile()...
 */

/*
 * Include necessary headers...
 */

#include "mxml.h"


/*
 * Local functions...
 */

mxml_type_t	type_cb(mxml_node_t *node);


/*
 * 'main()' - Main entry for test program.
 */

int					/* O - Exit status */
main(int  argc,				/* I - Number of command-line args */
     char *argv[])			/* I - Command-line args */
{
  FILE		*fp;			/* File to read */
Michael R Sweet's avatar
Michael R Sweet committed
47 48
  mxml_node_t	*tree,			/* XML tree */
		*node;			/* Node which should be in test.xml */
Michael R Sweet's avatar
Michael R Sweet committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84


 /*
  * Check arguments...
  */

  if (argc != 2)
  {
    fputs("Usage: testmxml filename.xml\n", stderr);
    return (1);
  }

 /*
  * Open the file...
  */

  if ((fp = fopen(argv[1], "r")) == NULL)
  {
    perror(argv[1]);
    return (1);
  }

 /*
  * Read the file...
  */

  tree = mxmlLoadFile(NULL, fp, type_cb);

  fclose(fp);

  if (!tree)
  {
    fputs("Unable to read XML file!\n", stderr);
    return (1);
  }

Michael R Sweet's avatar
Michael R Sweet committed
85
  if (!strcmp(argv[1], "test.xml"))
Michael R Sweet's avatar
Michael R Sweet committed
86
  {
Michael R Sweet's avatar
Michael R Sweet committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
   /*
    * Verify that mxmlFindElement() and indirectly mxmlWalkNext() work
    * properly...
    */

    if ((node = mxmlFindElement(tree, tree, "choice")) == NULL)
    {
      fputs("Unable to find first <choice> element in XML tree!\n", stderr);
      mxmlDelete(tree);
      return (1);
    }

    if ((node = mxmlFindElement(node, tree, "choice")) == NULL)
    {
      fputs("Unable to find second <choice> element in XML tree!\n", stderr);
      mxmlDelete(tree);
      return (1);
    }
Michael R Sweet's avatar
Michael R Sweet committed
105 106
  }

Michael R Sweet's avatar
Michael R Sweet committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
 /*
  * Print the XML tree...
  */

  mxmlSaveFile(tree, stdout);
  puts("");

 /*
  * Delete the tree and return...
  */

  mxmlDelete(tree);

  return (0);
}


/*
 * 'type_cb()' - XML data type callback for mxmlLoadFile()...
 */

mxml_type_t				/* O - Data type */
type_cb(mxml_node_t *node)		/* I - Element node */
{
  const char	*type;			/* Type string */


 /*
  * You can lookup attributes and/or use the element name, hierarchy, etc...
  */

  if ((type = mxmlElementGetAttr(node, "type")) == NULL)
    type = node->value.element.name;

  if (!strcmp(type, "integer"))
    return (MXML_INTEGER);
  else if (!strcmp(type, "opaque"))
    return (MXML_OPAQUE);
  else if (!strcmp(type, "real"))
    return (MXML_REAL);
  else
    return (MXML_TEXT);
}


/*
Michael R Sweet's avatar
Michael R Sweet committed
153
 * End of "$Id: testmxml.c,v 1.4 2003/06/04 01:23:21 mike Exp $".
Michael R Sweet's avatar
Michael R Sweet committed
154
 */