EXPAT (XML Parsing Library) and expatxml Parsing Library

Source: Internet
Author: User

EXPAT (XML Parsing Library) and expatxml Parsing Library

I. Introduction

Expat is an XML parsing library written in C language. James Clark created this library and is now the technical leader of the W3 organization that develops XML standards. The current version is 2.0. The development team led by Clark Cooper was responsible for development at sourceforge.net from 2.0.

Expat is a lightweight XML interpreter based on the SAX model that is not verified (by default, v1.2 starts to provide verification interfaces and needs to be manually processed. Currently, XML parsing mainly involves two models: SAX and DOM. Specifically, SAX (Simple API for XML) is an event-based parsing method. The basic principle is to analyze the XML document and notify the user of the resolution result by triggering an event. In this way, the memory usage is small and the speed is fast, but the user program is complicated accordingly. The DOM (incluentobject Model) analyzes the entire XML document at one time and stores the parsing results in a tree structure in the memory. At the same time, it provides users with a series of interfaces to access and edit the tree structure. This method occupies a large amount of memory and is often slower than SAX. However, it can provide users with an object-oriented access interface, which is more user-friendly.

For a specific XML document, its correctness is divided into two levels. First, the format must comply with the basic XML format requirements. For example, the first line must have a declaration, and the label nesting layers must be consistent, is a qualified XML file called well-formatted. However, an XML document must meet the corresponding standards in terms of semantics because of its different content. These standards are defined by the corresponding DTD file or Schema file, XML files that meet these definition requirements are called valid.

Therefore, the parser can be divided into two types: verification and non-verification. Yes. It verifies the XML file with the corresponding DTD file according to the declaration in the XML file to check whether it meets the requirements of the DTD file. For non-validation purposes, the DTD file is ignored and can be parsed as long as the basic format is correct. Common XML parsing libraries are summarized as follows:

 

Reference: http://www.xml.com/pub/a/1999/09/expat/index.html

 

Ii. API

http://blog.csdn.net/ymj7150697/article/details/7384126http://blog.csdn.net/exclusivepig/article/details/4566252

Download expat-1.2.tar.gz

ftp://ftp.jclark.com/pub/xml/

 

Iii. Instances

Example 1: example1.c

#include <stdio.h>#include <stdlib.h>#include "xmlparse.h"void startElement (void *userData, const char *name, const char **atts){    int i;    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;}void endElement (void *userData, const char *name){    int *depthPtr = userData;    *depthPtr -= 1;}int main (int argc, char *argv[]){    char buf[BUFSIZ];    XML_Parser parser = XML_ParserCreate (NULL);    int done;    int depth = 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.