First, Introduction
Expat is an XML parsing library written in C. James Clark created this library and is now the technical leader of the W3 organization that formulates the XML standard. The current version is 2.0. The development team, led by Clark Cooper, was responsible for the development at SourceForge.net starting from 2.0.
Expat is a lightweight XML interpreter based on a sax model, non-validating (by default, v1.2 begins to provide a validation interface that requires manual processing by the user). There are two major models of XML parsing: Sax and Dom. where sax (simple API for XML) is an event-based parsing method. The basic working principle is to parse an XML document to notify the user of the result of the resolution by triggering an event. This method consumes less memory and is fast, but the user program is more complex. In the case of Dom (DocumentObject Model), the entire XML document is parsed at once, and the parsed results are saved in memory in a tree structure. At the same time, a series of interfaces are provided to the user to access and edit the tree structure. This approach consumes large memory and is often slower than sax, but it gives the user an object-oriented access interface that is more user-friendly.
For a particular XML document, its correctness is divided into two levels. The first is that the format should conform to the basic XML format requirements, such as the first line to have a declaration, the nesting level of tags must be consistent, and so on, to meet these requirements of the file, is a qualified XML file, called well-formatted. But beyond that, an XML document must be semantically consistent with its content, defined by the corresponding DTD file or schema file, which conforms to the requirements of these definitions of XML files, called valid.
Therefore, the parser is also divided into two types: Authentication and non-authentication. is a validation of the declaration in the XML file, using the corresponding DTD file to verify the XML file, to check whether it satisfies the requirements of the DTD file. Non-verifiable DTD files are ignored and can be parsed as long as the basic format is correct. The common XML parsing libraries are summarized as follows:
Reference: http://www.xml.com/pub/a/1999/09/expat/index.html
Second, the API
http://blog.csdn.net/ymj7150697/article/details/7384126http://blog.csdn.net/ exclusivepig/article/details/4566252
expat-1.2.tar.gz Download
FTP://ftp.jclark.com/pub/xml/
Third, examples
Example 1:example1.c
#include <stdio.h>#include<stdlib.h>#include"xmlparse.h"voidStartelement (void*userdata,Const Char*name,Const Char**Atts) { inti; int*depthptr =UserData; for(i =0; i < *depthptr; i++) printf ("\ t"); puts (name); for(i =0; Atts[i]; i + =2) {printf ("\t%s= '%s '", Atts[i], atts[i +1]); } printf ("\ n"); *depthptr + =1;}voidEndElement (void*userdata,Const Char*name) { int*depthptr =UserData; *depthptr-=1;}intMain (intargcChar*argv[]) { CharBuf[bufsiz]; Xml_parser Parser=xml_parsercreate (NULL); intDone ; intdepth =0; FILE*FP; if(ARGC! =2) {printf ("Usage:%s filename\n", argv[0]); Exit (0); } if(fp = fopen (argv[1],"R")) ==NULL) {printf ("Can ' t open%s\n", argv[1]); Exit (1); } xml_setuserdata (Parser,&depth); Xml_setelementhandler (parser, startelement, endElement); Do{size_t len= Fread (buf,1,sizeof(BUF), FP); //Done = Len < sizeof (BUF);Done =feof (FP); if(!xml_parse (parser, buf, Len, done)) {fprintf (stderr,"%s at line%d\n", Xml_errorstring (Xml_geterrorcode (parser)), Xml_getcurrentlinenumber (parser)); return 1; } } while(!Done ); Xml_parserfree (parser); return 0;}
Compile
gcc -g-o example1 example1.c-i/root/srcpkg/expat-1.2/xmlparse-l/root/srcpkg/expat-1.2/ Xmlparse-static-lexpat
Run
EXPAT (XML parsing library)