⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testmxml.c

📁 minixml2.5最新的版本。 嵌入式xml 解析、查找、生成、遍历 功能,全部实现是标准c,移植很容易。 最新的2.5
💻 C
📖 第 1 页 / 共 2 页
字号:
    return (1);  }  mxmlIndexDelete(ind); /*  * Check the mxmlDelete() works properly...  */  for (i = 0; i < 8; i ++)  {    if (tree->child)      mxmlDelete(tree->child);    else    {      fprintf(stderr, "ERROR: Child pointer prematurely NULL on child #%d\n",              i + 1);      mxmlDelete(tree);      return (1);    }  }  if (tree->child)  {    fputs("ERROR: Child pointer not NULL after deleting all children!\n", stderr);    return (1);  }  if (tree->last_child)  {    fputs("ERROR: Last child pointer not NULL after deleting all children!\n", stderr);    return (1);  }  mxmlDelete(tree); /*  * Open the file...  */  if (argv[1][0] == '<')    tree = mxmlLoadString(NULL, argv[1], type_cb);  else if ((fp = fopen(argv[1], "rb")) == NULL)  {    perror(argv[1]);    return (1);  }  else  {   /*    * Read the file...    */    tree = mxmlLoadFile(NULL, fp, type_cb);    fclose(fp);  }  if (!tree)  {    fputs("Unable to read XML file!\n", stderr);    return (1);  }  if (!strcmp(argv[1], "test.xml"))  {   /*    * Verify that mxmlFindElement() and indirectly mxmlWalkNext() work    * properly...    */    if ((node = mxmlFindElement(tree, tree, "choice", NULL, NULL,                                MXML_DESCEND)) == NULL)    {      fputs("Unable to find first <choice> element in XML tree!\n", stderr);      mxmlDelete(tree);      return (1);    }    if ((node = mxmlFindElement(node, tree, "choice", NULL, NULL,                                MXML_NO_DESCEND)) == NULL)    {      fputs("Unable to find second <choice> element in XML tree!\n", stderr);      mxmlDelete(tree);      return (1);    }  } /*  * Print the XML tree...  */  mxmlSaveFile(tree, stdout, whitespace_cb); /*  * Save the XML tree to a string and print it...  */  if (mxmlSaveString(tree, buffer, sizeof(buffer), whitespace_cb) > 0)    fputs(buffer, stderr); /*  * Delete the tree...  */  mxmlDelete(tree); /*  * Read from/write to file descriptors...  */  if (argv[1][0] != '<')  {   /*    * Open the file again...    */    if ((fd = open(argv[1], O_RDONLY | O_BINARY)) < 0)    {      perror(argv[1]);      return (1);    }   /*    * Read the file...    */    tree = mxmlLoadFd(NULL, fd, type_cb);    close(fd);   /*    * Create filename.xmlfd...    */    snprintf(buffer, sizeof(buffer), "%sfd", argv[1]);    if ((fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666)) < 0)    {      perror(buffer);      mxmlDelete(tree);      return (1);    }   /*    * Write the file...    */    mxmlSaveFd(tree, fd, whitespace_cb);    close(fd);   /*    * Delete the tree...    */    mxmlDelete(tree);  } /*  * Test SAX methods...  */  memset(event_counts, 0, sizeof(event_counts));  if (argv[1][0] == '<')    tree = mxmlSAXLoadString(NULL, argv[1], type_cb, sax_cb, NULL);  else if ((fp = fopen(argv[1], "rb")) == NULL)  {    perror(argv[1]);    return (1);  }  else  {   /*    * Read the file...    */    tree = mxmlSAXLoadFile(NULL, fp, type_cb, sax_cb, NULL);    fclose(fp);  }  if (!strcmp(argv[1], "test.xml"))  {    if (event_counts[MXML_SAX_CDATA] != 1)    {      fprintf(stderr, "MXML_SAX_CDATA seen %d times, expected 1 times!\n",              event_counts[MXML_SAX_CDATA]);      return (1);    }    if (event_counts[MXML_SAX_COMMENT] != 1)    {      fprintf(stderr, "MXML_SAX_COMMENT seen %d times, expected 1 times!\n",              event_counts[MXML_SAX_COMMENT]);      return (1);    }    if (event_counts[MXML_SAX_DATA] != 61)    {      fprintf(stderr, "MXML_SAX_DATA seen %d times, expected 61 times!\n",              event_counts[MXML_SAX_DATA]);      return (1);    }    if (event_counts[MXML_SAX_DIRECTIVE] != 1)    {      fprintf(stderr, "MXML_SAX_DIRECTIVE seen %d times, expected 1 times!\n",              event_counts[MXML_SAX_DIRECTIVE]);      return (1);    }    if (event_counts[MXML_SAX_ELEMENT_CLOSE] != 20)    {      fprintf(stderr, "MXML_SAX_ELEMENT_CLOSE seen %d times, expected 20 times!\n",              event_counts[MXML_SAX_ELEMENT_CLOSE]);      return (1);    }    if (event_counts[MXML_SAX_ELEMENT_OPEN] != 20)    {      fprintf(stderr, "MXML_SAX_ELEMENT_OPEN seen %d times, expected 20 times!\n",              event_counts[MXML_SAX_ELEMENT_OPEN]);      return (1);    }  } /*  * Return...  */  return (0);}/* * 'sax_cb()' - Process nodes via SAX. */voidsax_cb(mxml_node_t      *node,		/* I - Current node */       mxml_sax_event_t event,		/* I - SAX event */       void             *data)		/* I - SAX user data */{ /*  * This SAX callback just counts the different events.  */  event_counts[event] ++;}/* * '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") || !strcmp(type, "pre"))    return (MXML_OPAQUE);  else if (!strcmp(type, "real"))    return (MXML_REAL);  else    return (MXML_TEXT);}/* * 'whitespace_cb()' - Let the mxmlSaveFile() function know when to insert *                     newlines and tabs... */const char *				/* O - Whitespace string or NULL */whitespace_cb(mxml_node_t *node,	/* I - Element node */              int         where)	/* I - Open or close tag? */{  mxml_node_t	*parent;		/* Parent node */  int		level;			/* Indentation level */  const char	*name;			/* Name of element */  static const char *tabs = "\t\t\t\t\t\t\t\t";					/* Tabs for indentation */ /*  * We can conditionally break to a new line before or after any element.  * These are just common HTML elements...  */  name = node->value.element.name;  if (!strcmp(name, "html") || !strcmp(name, "head") || !strcmp(name, "body") ||      !strcmp(name, "pre") || !strcmp(name, "p") ||      !strcmp(name, "h1") || !strcmp(name, "h2") || !strcmp(name, "h3") ||      !strcmp(name, "h4") || !strcmp(name, "h5") || !strcmp(name, "h6"))  {   /*    * Newlines before open and after close...    */    if (where == MXML_WS_BEFORE_OPEN || where == MXML_WS_AFTER_CLOSE)      return ("\n");  }  else if (!strcmp(name, "dl") || !strcmp(name, "ol") || !strcmp(name, "ul"))  {   /*    * Put a newline before and after list elements...    */    return ("\n");  }  else if (!strcmp(name, "dd") || !strcmp(name, "dt") || !strcmp(name, "li"))  {   /*    * Put a tab before <li>'s, <dd>'s, and <dt>'s, and a newline after them...    */    if (where == MXML_WS_BEFORE_OPEN)      return ("\t");    else if (where == MXML_WS_AFTER_CLOSE)      return ("\n");  }  else if (!strncmp(name, "?xml", 4))  {    return (NULL);  }  else if (where == MXML_WS_BEFORE_OPEN ||           ((!strcmp(name, "choice") || !strcmp(name, "option")) &&	    where == MXML_WS_BEFORE_CLOSE))  {    for (level = -1, parent = node->parent;         parent;	 level ++, parent = parent->parent);    if (level > 8)      level = 8;    else if (level < 0)      level = 0;    return (tabs + 8 - level);  }  else if (where == MXML_WS_AFTER_CLOSE ||           ((!strcmp(name, "group") || !strcmp(name, "option") ||	     !strcmp(name, "choice")) &&            where == MXML_WS_AFTER_OPEN))    return ("\n");  else if (where == MXML_WS_AFTER_OPEN && !node->child)    return ("\n"); /*  * Return NULL for no added whitespace...  */  return (NULL);}/* * End of "$Id: testmxml.c 270 2007-04-23 21:48:03Z mike $". */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -