From: Fred L. Drake, Jr. The bulk of this document was originally commissioned as an article by
XML.com. They graciously allowed
-Clark Cooper to retain copyright and to distribute it with expat.
The expat distribution comes as a compressed (with GNU gzip) tar +
The Expat distribution comes as a compressed (with GNU gzip) tar file. You may download the latest version from Source Forge. After unpacking this, cd into the directory. Then follow either the Win32 @@ -225,7 +225,7 @@ Developer Studio installed, then from Windows Explorer double-click on manner.
Alternatively, you may download the Win32 binary package that -contains the expat.h include file and a pre-built DLL.
+contains the "expat.h" include file and a pre-built DLL.If you're happy with all the defaults that configure picks for you, and you have permission on your system to install into /usr/local, you -can install expat with this sequence of commands:
+can install Expat with this sequence of commands:./configure @@ -258,20 +258,21 @@ library and header would get installed in
Using Expat
-Compiling and Linking against expat
+Compiling and Linking Against Expat
-Unless you installed expat in a location not expected by your -compiler and linker, all you have to do to use expat in your programs -is to include the expat header (
#include <expat.h>
) +Unless you installed Expat in a location not expected by your +compiler and linker, all you have to do to use Expat in your programs +is to include the Expat header (
+needs to link against the Expat library. On Unix systems, this would +usually be done with the#include <expat.h>
) in your files that make calls to it and to tell the linker that it -needs to link against the expat library. On Unix systems, this would -be the-lexpat
argument. Otherwise, you'll need to tell -the compiler where to look for the expat header and the linker where -to find the expat library. You may also need to take steps to tell the -operating system where to find this libary at run time.-lexpat
argument. Otherwise, +you'll need to tell the compiler where to look for the Expat header +and the linker where to find the Expat library. You may also need to +take steps to tell the operating system where to find this libary at +run time.On a Unix-based system, here's what a Makefile might look like when -expat is installed in a standard location:
+Expat is installed in a standard location:CC=cc @@ -281,7 +282,7 @@ xmlapp: xmlapp.o $(CC) $(LDFLAGS) -o xmlapp xmlapp.o $(LIBS)-If you installed expat in, say,
/home/me/mystuff
, then +If you installed Expat in, say,
/home/me/mystuff
, then the Makefile would look like this:@@ -302,13 +303,13 @@ your application.-Expat Basics
As we saw in the example in the overview, the first step in parsing -an XML document with expat is to create a parser object. There are three functions in the expat API for creating a +an XML document with Expat is to create a parser object. There are three functions in the Expat API for creating a parser object. However, only two of these (
XML_ParserCreate
andXML_ParserCreateNS
) can be used for constructing a parser for a top-level document. The object returned -by these functions is an opaque pointer (i.e. expat.h declares it as +by these functions is an opaque pointer (i.e. "expat.h" declares it as void *) to data with further internal structure. In order to free the memory associated with this object you must callXML_ParserFree
. Note that if you have @@ -397,7 +398,7 @@ handler.In order to be able to pass information between different handlers without using globals, you'll need to define a data structure to hold -the shared variables. You can then tell expat (with the
@@ -445,8 +446,8 @@ xmldecl_handler(void *userData,XML_SetUserData
function) to pass a pointer to this structure to the handlers. This is typically the first argument received by most handlers.Namespace Processing
When the parser is created using the
<?xml encoding="Big5"?>XML_ParserCreateNS
, function, expat -performs namespace processing. Under namespace processing, expat +"#XML_ParserCreateNS" >XML_ParserCreateNS, function, Expat +performs namespace processing. Under namespace processing, Expat consumesxmlns
andxmlns:...
attributes, which declare namespaces for the scope of the element in which they occur. This means that your start handler will not see these @@ -506,13 +507,13 @@ looks like an XML declaration with just an encoding declaration:With expat, you may also specify an encoding at the time of +
With Expat, you may also specify an encoding at the time of creating a parser. This is useful when the encoding information may come from a source outside the document itself (like a higher level protocol.)
+in Expat:
XML_Encoding
structure. This
function should return the Unicode scalar value for the sequence or -1
if the sequence is malformed.
-One pitfall that novice expat users are likely to fall into is that -although expat may accept input in various encodings, the strings that -it passes to the handlers are always encoded in UTF-8. Your -application is responsible for any translation of these strings into -other encodings.
+One pitfall that novice Expat users are likely to fall into is that +although Expat may accept input in various encodings, the strings that +it passes to the handlers are always encoded in UTF-8 or UTF-16 +(depending on how Expat was compiled). Your application is responsible +for any translation of these strings into other encodings.
len
to read a piece of the document
-into. A NULL value is returned if expat can't allocate enough memory for
+into. A NULL value is returned if Expat can't allocate enough memory for
this buffer. This has to be called prior to every call to
XML_ParseBuffer
. A
typical use would look like this:
@@ -1675,11 +1676,11 @@ XML_LChar *
XML_ExpatVersion();
"expat_1.95.1"
).
-struct XML_expat_version +struct XML_Expat_Version XML_ExpatVersionInfo();