Expat http://download1.csdn.net/down3/20070607/07111232845.gz recognized from PHP
- Expat is an XML parser that allows PHP programs to parse the structure and content of XML files.
- Generally, the XML parser is divided into two basic types.
- Parse Parser (tree-based parser): the XML file is converted into a parse parser structure. this parser analyzes the entire article, and provides an API to ask every element of the product at the same time. its General Mark is Dom (file object mode)
- Event-based Parser: uploads XML files as a series of events to handler. when a special event occurs, the parser goes through the function provided by the program.
- The event parser has a data-centric view (data-centric view) of the XML file, that is, it is concentrated in the XML file's data part, rather than its structure. these parsers process files from the beginning to the end, and events like the start of an element, the end of an element, and the start of a feature are overwritten (callback) send a message to the application.
- Expat is a type of event parser.
- Expat is a parser that does not determine whether the XML file is valid. Therefore, you can ignore any DTD related to the file, however, the XML file format still needs to be complete (indicating that the DTD still exists). Otherwise, expat (the same as other sdks conforming to the XML standard) it will be suspended due to outbound traffic.
- Example:
Examples directly handled by scew(scew-0.3.2.tar.gz)
- Scew_print.c: used to describe the XML Scheme of the specified number.
- Scew_write.c: used to export the XML file of the specified data.
/** * * @file scew_print.c * @author Aleix Conchillo Flaque <aleix@member.fsf.org> * @date Wed Dec 04, 2002 01:11 * @brief SCEW usage example * * $Id: scew_print.c,v 1.20 2004/01/29 22:38:33 aleix Exp $ * * @if copyright * * Copyright (C) 2002, 2003, 2004 Aleix Conchillo Flaque * * SCEW is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * SCEW 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * * @endif * * This example shows the usage of the API provided by SCEW. It will * print an XML file given as the first program parameter. *//** * You will probably need to change this include to <scew/scew.h> in you * program. */#include <scew/scew.h>#include <stdio.h>/* indentation size (in whitespaces) */int const indent_size = 4;voidprint_indent(unsigned int indent){ if (indent > 0) { printf("%*s", indent * indent_size, " "); }}voidprint_attributes(scew_element* element){ scew_attribute* attribute = NULL; if (element != NULL) { /** * Iterates through the element's attribute list, printing the * pair name-value. */ attribute = NULL; while ((attribute = scew_attribute_next(element, attribute)) != NULL) { printf(" %s=/"%s/"", scew_attribute_name(attribute), scew_attribute_value(attribute)); } }}voidprint_element(scew_element* element, unsigned int indent){ scew_element* child = NULL; XML_Char const* contents = NULL; if (element == NULL) { return; } /** * Prints the starting element tag with its attributes. */ print_indent(indent); printf("<%s", scew_element_name(element)); print_attributes(element); printf(">"); contents = scew_element_contents(element); if (contents == NULL) { printf("/n"); } /** * Call print_element function again for each child of the * current element. */ child = NULL; while ((child = scew_element_next(element, child)) != NULL) { print_element(child, indent + 1); } /* Prints element's content. */ if (contents != NULL) { printf("%s", contents); } else { print_indent(indent); } /** * Prints the closing element tag. */ printf("</%s>/n", scew_element_name(element));}intmain(int argc, char** argv){ scew_tree* tree = NULL; scew_parser* parser = NULL; if (argc < 2) { printf("usage: scew_print file.xml/n"); return EXIT_FAILURE; } /** * Creates an SCEW parser. This is the first function to call. */ parser = scew_parser_create(); scew_parser_ignore_whitespaces(parser, 1); /* Loads an XML file */ if (!scew_parser_load_file(parser, argv[1])) { scew_error code = scew_error_code(); printf("Unable to load file (error #%d: %s)/n", code, scew_error_string(code)); if (code == scew_error_expat) { enum XML_Error expat_code = scew_error_expat_code(parser); printf("Expat error #%d (line %d, column %d): %s/n", expat_code, scew_error_expat_line(parser), scew_error_expat_column(parser), scew_error_expat_string(expat_code)); } return EXIT_FAILURE; } tree = scew_parser_tree(parser); /* Prints full tree */ print_element(scew_tree_root(tree), 0); /* Remember to free tree (scew_parser_free does not free it) */ scew_tree_free(tree); /* Frees the SCEW parser */ scew_parser_free(parser); return 0;}
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
/** * * @file scew_write.c * @author Aleix Conchillo Flaque <aleix@member.fsf.org> * @date Sun Mar 30, 2003 12:21 * @brief SCEW usage example * * $Id: scew_write.c,v 1.5 2004/01/29 22:38:34 aleix Exp $ * * @if copyright * * Copyright (C) 2002, 2003, 2004 Aleix Conchillo Flaque * * SCEW is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * SCEW 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * * @endif * * This example shows the usage of the API provided by SCEW. It will * create a new XML and write it to a file. * * We will create an XML with the follwing structure: * * <scew_test> * <element> * element contents. * </element> * <element attribute="value"/> * <element attribute1="value1" attribute2="value2"/> * <element> * <sub_element attribute="value"/> * <sub_element attribute1="value1" attribute2="value2"> * <sub_sub_element attribute="value"> * element contents. * </sub_sub_element> * </sub_element> * </element> * </scew_test> *//** * You will probably need to change this include to <scew/scew.h> in you * program. */#include <scew/scew.h>#include <stdio.h>intmain(int argc, char** argv){ scew_tree* tree = NULL; scew_element* root = NULL; scew_element* element = NULL; scew_element* sub_element = NULL; scew_element* sub_sub_element = NULL; scew_attribute* attribute = NULL; if (argc < 2) { printf("usage: scew_write new_file.xml/n"); return EXIT_FAILURE; } /** * Create an empty XML tree in memory, and add a root element * "scew_test". */ tree = scew_tree_create(); root = scew_tree_add_root(tree, "scew_test"); /* Add an element and set element contents. */ element = scew_element_add(root, "element"); scew_element_set_contents(element, "element contents."); /* Add an element with an attribute pair (name, value). */ element = scew_element_add(root, "element"); scew_element_add_attr_pair(element, "attribute", "value"); element = scew_element_add(root, "element"); scew_element_add_attr_pair(element, "attribute1", "value1"); /** * Another way to add an attribute. You loose attribute ownership, * so there is no need to free it. */ attribute = scew_attribute_create("attribute2", "value2"); scew_element_add_attr(element, attribute); element = scew_element_add(root, "element"); sub_element = scew_element_add(element, "sub_element"); scew_element_add_attr_pair(sub_element, "attribute", "value"); sub_element = scew_element_add(element, "sub_element"); scew_element_add_attr_pair(sub_element, "attribute1", "value1"); scew_element_add_attr_pair(sub_element, "attribute2", "value2"); sub_sub_element = scew_element_add(sub_element, "sub_sub_element"); scew_element_add_attr_pair(sub_sub_element, "attribute", "value"); scew_element_set_contents(sub_sub_element, "element contents."); /** * Save an XML tree to a file. */ if (!scew_writer_tree_file(tree, argv[1])) { printf("Unable to create %s/n", argv[1]); return EXIT_FAILURE; } /* Frees the SCEW tree */ scew_tree_free(tree); return 0;}