blob: 71a6c5f887c8b797259622ae6874450a9dd805d1 [file] [log] [blame]
Daniel Veillard96984452000-08-31 13:50:12 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
2 "http://www.w3.org/TR/REC-html40/loose.dtd">
3<html>
4<head>
5 <title>Libxml Input/Output handling</title>
6 <meta name="GENERATOR" content="amaya V3.2.1">
7 <meta http-equiv="Content-Type" content="text/html">
8</head>
9
10<body bgcolor="#ffffff">
11<h1 align="center">Libxml Input/Output handling</h1>
12
13<p>Location: <a
14href="http://xmlsoft.org/xmlio.html">http://xmlsoft.org/xmlio.html</a></p>
15
16<p>Libxml home page: <a href="http://xmlsoft.org/">http://xmlsoft.org/</a></p>
17
18<p>Mailing-list archive: <a
19href="http://xmlsoft.org/messages/">http://xmlsoft.org/messages/</a></p>
20
Daniel Veillard2bb89092000-08-31 14:57:50 +000021<p>Version: $Revision: 1.2 $</p>
Daniel Veillard96984452000-08-31 13:50:12 +000022
23<p>Table of Content:</p>
24<ol>
25 <li><a href="#General">General overview</a></li>
26 <li><a href="#basic">The basic buffer type</a></li>
27 <li><a href="#Input">Input I/O handlers</a></li>
28 <li><a href="#Output">Output I/O handlers</a></li>
Daniel Veillard2bb89092000-08-31 14:57:50 +000029 <li><a href="#entities">The entities loader</a></li>
Daniel Veillard96984452000-08-31 13:50:12 +000030 <li><a href="#Example">Example of customized I/O</a></li>
31</ol>
32
33<h2><a name="General">General overview</a></h2>
34
35<p>The module <code><a
36href="http://xmlsoft.org/html/gnome-xml-xmlio.html">xmlIO.h</a></code>
Daniel Veillard2bb89092000-08-31 14:57:50 +000037provides the interfaces to the libxml I/O system. This consists of 4 main
Daniel Veillard96984452000-08-31 13:50:12 +000038parts:</p>
39<ul>
Daniel Veillard2bb89092000-08-31 14:57:50 +000040 <li>Entities loader, this is a routine which tries to fetch the entities
41 (files) based on their PUBLIC and SYSTEM identifiers. The default loader
42 don't look at the public identifier since libxml do not maintain a
43 catalog. You can redefine you own entity loader by using
44 <code>xmlGetExternalEntityLoader()</code> and
45 <code>xmlSetExternalEntityLoader()</code>. <a href="#entities">Check the
46 example</a>.</li>
Daniel Veillard96984452000-08-31 13:50:12 +000047 <li>Input I/O buffers which are a commodity structure used by the parser(s)
48 input layer to handle fetching the informations to feed the parser. This
49 provides buffering and is also a placeholder where the encoding convertors
50 to UTF8 are piggy-backed.</li>
51 <li>Output I/O buffers are similar to the Input ones and fulfill similar
52 task but when generating a serialization from a tree.</li>
53 <li>A mechanism to register sets of I/O callbacks and associate them with
54 specific naming schemes like the protocol part of the URIs.
55 <p>This affect the default I/O operations and allows to use specific I/O
56 handlers for certain names.</p>
57 </li>
58</ul>
59
Daniel Veillard2bb89092000-08-31 14:57:50 +000060<p>The general mechanism used when loading http://rpmfind.net/xml.html for
Daniel Veillard96984452000-08-31 13:50:12 +000061example in the HTML parser is the following:</p>
62<ol>
Daniel Veillard2bb89092000-08-31 14:57:50 +000063 <li>The default entity loader calls <code>xmlNewInputFromFile()</code> with
64 the parsing context and the URI string.</li>
Daniel Veillard96984452000-08-31 13:50:12 +000065 <li>the URI string is checked against the existing registered handlers using
66 their match() callback function, if the HTTP module was compiled in, it is
Daniel Veillard2bb89092000-08-31 14:57:50 +000067 registered and its match() function will succeeds</li>
Daniel Veillard96984452000-08-31 13:50:12 +000068 <li>the open() function of the handler is called and if successful will
69 return an I/O Input buffer</li>
70 <li>the parser will the start reading from this buffer and progressively
71 fetch information from the resource, calling the read() function of the
72 handler until the resource is exhausted</li>
73 <li>if an encoding change is detected it will be installed on the input
74 buffer, providing buffering and efficient use of the conversion
75 routines</li>
76 <li>once the parser has finished, the close() function of the handler is
77 called once and the Input buffer and associed resources are
78 deallocated.</li>
79</ol>
80
81<p>The user defined callbacks are checked first to allow overriding of the
82default libxml I/O routines.</p>
83
84<h2><a name="basic">The basic buffer type</a></h2>
85
86<p>All the buffer manipulation handling is done using the
87<code>xmlBuffer</code> type define in <code><a
88href="http://xmlsoft.org/html/gnome-xml-tree.html">tree.h</a> </code>which is
89a resizable memory buffer. The buffer allocation strategy can be selected to
90be either best-fit or use an exponential doubling one (CPU vs. memory use
91tradeoff). The values are <code>XML_BUFFER_ALLOC_EXACT</code> and
92<code>XML_BUFFER_ALLOC_DOUBLEIT</code>, and can be set individually or on a
93system wide basis using <code>xmlBufferSetAllocationScheme()</code>. A number
94of functions allows to manipulate buffers with names starting with the
95<code>xmlBuffer...</code> prefix.</p>
96
97<h2><a name="Input">Input I/O handlers</a></h2>
98
99<p>An Input I/O handler is a simple structure
100<code>xmlParserInputBuffer</code> containing a context associated to the
101resource (file descriptor, or pointer to a protocol handler), the read() and
102close() callbacks to use and an xmlBuffer. And extra xmlBuffer and a charset
103encoding handler are also present to support charset conversion when
104needed.</p>
105
106<h2><a name="Output">Output I/O handlers</a></h2>
107
108<p>An Output handler <code>xmlOutputBuffer</code> is completely similar to an
109Input one except the callbacks are write() and close().</p>
110
Daniel Veillard2bb89092000-08-31 14:57:50 +0000111<h2><a name="entities">The entities loader</a></h2>
112
113<p>The entity loader resolves requests for new entities and create inputs for
114the parser. Creating an input from a filename or an URI string is done through
115the xmlNewInputFromFile() routine. The default entity loader do not handle
116the PUBLIC identifier associated with an entity (if any). So it just calls
117xmlNewInputFromFile() with the SYSTEM identifier (which is mandatory in
118XML).</p>
119
120<p>If you want to hook up a catalog mechanism then you simply need to override
121the default entity loader, here is an example:</p>
122<pre>#include &lt;libxml/xmlIO.h&gt;
123
124xmlExternalEntityLoader defaultLoader = NULL;
125
126xmlParserInputPtr
127xmlMyExternalEntityLoader(const char *URL, const char *ID,
128 xmlParserCtxtPtr ctxt) {
129 xmlParserInputPtr ret;
130 const char *fileID = NULL;
131 /* lookup for the fileID depending on ID */
132
133 ret = xmlNewInputFromFile(ctxt, fileID);
134 if (ret != NULL)
135 return(ret);
136 if (defaultLoader != NULL)
137 ret = defaultLoader(URL, ID, ctxt);
138 return(ret);
139}
140
141int main(..) {
142 ...
143
144 /*
145 * Install our own entity loader
146 */
147 defaultLoader = xmlGetExternalEntityLoader();
148 xmlSetExternalEntityLoader(xmlMyExternalEntityLoader);
149
150 ...
151}</pre>
152
Daniel Veillard96984452000-08-31 13:50:12 +0000153<h2><a name="Example">Example of customized I/O</a></h2>
154
155<p>This example come from <a href="http://xmlsoft.org/messages/0708.html">a
156real use case</a>, xmlDocDump() closes the FILE * passed by the application
157and this was a problem. The <a
158href="http://xmlsoft.org/messages/0711.html">solution</a> was to redefine a
159new output handler with the closing call deactivated:</p>
160<ol>
161 <li>First define a new I/O ouput allocator where the output don't close the
162 file:
Daniel Veillarda7ad4522000-08-31 14:19:54 +0000163 <pre>xmlOutputBufferPtr
164xmlOutputBufferCreateOwn(FILE *file, xmlCharEncodingHandlerPtr encoder) {
165    xmlOutputBufferPtr ret;
166    
167    if (xmlOutputCallbackInitialized == 0)
168        xmlRegisterDefaultOutputCallbacks();
169
170    if (file == NULL) return(NULL);
171    ret = xmlAllocOutputBuffer(encoder);
172    if (ret != NULL) {
173        ret-&gt;context = file;
174        ret-&gt;writecallback = xmlFileWrite;
175        ret-&gt;closecallback = NULL; /* No close callback */
176    }
177    return(ret); <br>
Daniel Veillard2bb89092000-08-31 14:57:50 +0000178
179
Daniel Veillard96984452000-08-31 13:50:12 +0000180} </pre>
181 </li>
182 <li>And then use it to save the document:
183 <pre>FILE *f;
Daniel Veillarda7ad4522000-08-31 14:19:54 +0000184xmlOutputBufferPtr output;
185xmlDocPtr doc;
Daniel Veillard96984452000-08-31 13:50:12 +0000186int res;
Daniel Veillarda7ad4522000-08-31 14:19:54 +0000187
Daniel Veillard96984452000-08-31 13:50:12 +0000188f = ...
189doc = ....
Daniel Veillard96984452000-08-31 13:50:12 +0000190
Daniel Veillarda7ad4522000-08-31 14:19:54 +0000191output = xmlOutputBufferCreateOwn(f, NULL);
192res = xmlSaveFileTo(output, doc, NULL);
Daniel Veillard96984452000-08-31 13:50:12 +0000193 </pre>
194 </li>
195</ol>
196
197<p><a href="mailto:Daniel.Veillard@w3.org">Daniel Veillard</a></p>
198
Daniel Veillard2bb89092000-08-31 14:57:50 +0000199<p>$Id: xmlio.html,v 1.2 2000/08/31 14:19:54 veillard Exp $</p>
Daniel Veillard96984452000-08-31 13:50:12 +0000200</body>
201</html>