blob: 9fd9e6870d93165adfdb4ac43efab10d4fecd4ae [file] [log] [blame]
Daniel Veillard43d3f612001-11-10 11:57:23 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
Daniel Veillard96984452000-08-31 13:50:12 +00002<html>
3<head>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +00004<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
5<style type="text/css"><!--
Daniel Veillard2c748c62002-01-16 15:37:50 +00006TD {font-size: 14pt; font-family: Verdana,Arial,Helvetica}
7BODY {font-size: 14pt; font-family: Verdana,Arial,Helvetica; margin-top: 2em; margin-left: 0em; margin-right: 0em}
8H1 {font-size: 20pt; font-family: Verdana,Arial,Helvetica}
9H2 {font-size: 18pt; font-family: Verdana,Arial,Helvetica}
10H3 {font-size: 16pt; font-family: Verdana,Arial,Helvetica}
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000011A:link, A:visited, A:active { text-decoration: underline }
12--></style>
13<title>I/O Interfaces</title>
Daniel Veillard96984452000-08-31 13:50:12 +000014</head>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000015<body bgcolor="#8b7765" text="#000000" link="#000000" vlink="#000000">
16<table border="0" width="100%" cellpadding="5" cellspacing="0" align="center"><tr>
17<td width="180">
18<a href="http://www.gnome.org/"><img src="smallfootonly.gif" alt="Gnome Logo"></a><a href="http://www.w3.org/Status"><img src="w3c.png" alt="W3C Logo"></a><a href="http://www.redhat.com/"><img src="redhat.gif" alt="Red Hat Logo"></a>
19</td>
20<td><table border="0" width="90%" cellpadding="2" cellspacing="0" align="center" bgcolor="#000000"><tr><td><table width="100%" border="0" cellspacing="1" cellpadding="3" bgcolor="#fffacd"><tr><td align="center">
21<h1>The XML C library for Gnome</h1>
22<h2>I/O Interfaces</h2>
23</td></tr></table></td></tr></table></td>
24</tr></table>
25<table border="0" cellpadding="4" cellspacing="0" width="100%" align="center"><tr><td bgcolor="#8b7765"><table border="0" cellspacing="0" cellpadding="2" width="100%"><tr>
26<td valign="top" width="200" bgcolor="#8b7765"><table border="0" cellspacing="0" cellpadding="1" width="100%" bgcolor="#000000"><tr><td>
27<table width="100%" border="0" cellspacing="1" cellpadding="3">
28<tr><td colspan="1" bgcolor="#eecfa1" align="center"><center><b>Main Menu</b></center></td></tr>
Daniel Veillard8acca112002-01-21 09:52:27 +000029<tr><td bgcolor="#fffacd"><ul>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000030<li><a href="index.html">Home</a></li>
31<li><a href="intro.html">Introduction</a></li>
32<li><a href="FAQ.html">FAQ</a></li>
33<li><a href="docs.html">Documentation</a></li>
34<li><a href="bugs.html">Reporting bugs and getting help</a></li>
35<li><a href="help.html">How to help</a></li>
36<li><a href="downloads.html">Downloads</a></li>
37<li><a href="news.html">News</a></li>
Daniel Veillard7b602b42002-01-08 13:26:00 +000038<li><a href="XMLinfo.html">XML</a></li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000039<li><a href="XSLT.html">XSLT</a></li>
40<li><a href="architecture.html">libxml architecture</a></li>
41<li><a href="tree.html">The tree output</a></li>
42<li><a href="interface.html">The SAX interface</a></li>
43<li><a href="xmldtd.html">Validation &amp; DTDs</a></li>
44<li><a href="xmlmem.html">Memory Management</a></li>
45<li><a href="encoding.html">Encodings support</a></li>
46<li><a href="xmlio.html">I/O Interfaces</a></li>
47<li><a href="catalog.html">Catalog support</a></li>
48<li><a href="library.html">The parser interfaces</a></li>
49<li><a href="entities.html">Entities or no entities</a></li>
50<li><a href="namespaces.html">Namespaces</a></li>
51<li><a href="upgrade.html">Upgrading 1.x code</a></li>
Daniel Veillard52dcab32001-10-30 12:51:17 +000052<li><a href="threads.html">Thread safety</a></li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000053<li><a href="DOM.html">DOM Principles</a></li>
54<li><a href="example.html">A real example</a></li>
55<li><a href="contribs.html">Contributions</a></li>
56<li>
57<a href="xml.html">flat page</a>, <a href="site.xsl">stylesheet</a>
58</li>
59</ul></td></tr>
60</table>
61<table width="100%" border="0" cellspacing="1" cellpadding="3">
Daniel Veillard3bf65be2002-01-23 12:36:34 +000062<tr><td colspan="1" bgcolor="#eecfa1" align="center"><center><b>API Indexes</b></center></td></tr>
63<tr><td bgcolor="#fffacd"><ul>
64<li><a href="APIconstructors.html">Constructors</a></li>
65<li><a href="APIfunctions.html">Functions/Types</a></li>
66<li><a href="APIfiles.html">Modules</a></li>
67<li><a href="APIsymbols.html">Symbols</a></li>
68</ul></td></tr>
69</table>
70<table width="100%" border="0" cellspacing="1" cellpadding="3">
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000071<tr><td colspan="1" bgcolor="#eecfa1" align="center"><center><b>Related links</b></center></td></tr>
Daniel Veillard8acca112002-01-21 09:52:27 +000072<tr><td bgcolor="#fffacd"><ul>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000073<li><a href="http://mail.gnome.org/archives/xml/">Mail archive</a></li>
74<li><a href="http://xmlsoft.org/XSLT/">XSLT libxslt</a></li>
Daniel Veillard4a859202002-01-08 11:49:22 +000075<li><a href="http://phd.cs.unibo.it/gdome2/">DOM gdome2</a></li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000076<li><a href="ftp://xmlsoft.org/">FTP</a></li>
77<li><a href="http://www.fh-frankfurt.de/~igor/projects/libxml/">Windows binaries</a></li>
Daniel Veillarddb9dfd92001-11-26 17:25:02 +000078<li><a href="http://garypennington.net/libxml2/">Solaris binaries</a></li>
Daniel Veillardc6271d22001-10-27 07:50:58 +000079<li><a href="http://bugzilla.gnome.org/buglist.cgi?product=libxml">Bug Tracker</a></li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000080</ul></td></tr>
81</table>
82</td></tr></table></td>
83<td valign="top" bgcolor="#8b7765"><table border="0" cellspacing="0" cellpadding="1" width="100%"><tr><td><table border="0" cellspacing="0" cellpadding="1" width="100%" bgcolor="#000000"><tr><td><table border="0" cellpadding="3" cellspacing="1" width="100%"><tr><td bgcolor="#fffacd">
Daniel Veillard96984452000-08-31 13:50:12 +000084<p>Table of Content:</p>
85<ol>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000086<li><a href="#General1">General overview</a></li>
87<li><a href="#basic">The basic buffer type</a></li>
88<li><a href="#Input">Input I/O handlers</a></li>
89<li><a href="#Output">Output I/O handlers</a></li>
90<li><a href="#entities">The entities loader</a></li>
91<li><a href="#Example2">Example of customized I/O</a></li>
Daniel Veillard96984452000-08-31 13:50:12 +000092</ol>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000093<h3><a name="General1">General overview</a></h3>
94<p>The module <code><a href="http://xmlsoft.org/html/libxml-xmlio.html">xmlIO.h</a></code> provides
95the interfaces to the libxml I/O system. This consists of 4 main parts:</p>
Daniel Veillard96984452000-08-31 13:50:12 +000096<ul>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000097<li>Entities loader, this is a routine which tries to fetch the entities
Daniel Veillard2bb89092000-08-31 14:57:50 +000098 (files) based on their PUBLIC and SYSTEM identifiers. The default loader
99 don't look at the public identifier since libxml do not maintain a
100 catalog. You can redefine you own entity loader by using
101 <code>xmlGetExternalEntityLoader()</code> and
Daniel Veillard9c466822001-10-25 12:03:39 +0000102 <code>xmlSetExternalEntityLoader()</code>. <a href="#entities">Check the
103 example</a>.</li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000104<li>Input I/O buffers which are a commodity structure used by the parser(s)
Daniel Veillard96984452000-08-31 13:50:12 +0000105 input layer to handle fetching the informations to feed the parser. This
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000106 provides buffering and is also a placeholder where the encoding
107 convertors to UTF8 are piggy-backed.</li>
108<li>Output I/O buffers are similar to the Input ones and fulfill similar
Daniel Veillard96984452000-08-31 13:50:12 +0000109 task but when generating a serialization from a tree.</li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000110<li>A mechanism to register sets of I/O callbacks and associate them with
Daniel Veillard96984452000-08-31 13:50:12 +0000111 specific naming schemes like the protocol part of the URIs.
112 <p>This affect the default I/O operations and allows to use specific I/O
113 handlers for certain names.</p>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000114</li>
Daniel Veillard96984452000-08-31 13:50:12 +0000115</ul>
Daniel Veillard2bb89092000-08-31 14:57:50 +0000116<p>The general mechanism used when loading http://rpmfind.net/xml.html for
Daniel Veillard96984452000-08-31 13:50:12 +0000117example in the HTML parser is the following:</p>
118<ol>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000119<li>The default entity loader calls <code>xmlNewInputFromFile()</code> with
Daniel Veillard2bb89092000-08-31 14:57:50 +0000120 the parsing context and the URI string.</li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000121<li>the URI string is checked against the existing registered handlers
122 using their match() callback function, if the HTTP module was compiled
123 in, it is registered and its match() function will succeeds</li>
124<li>the open() function of the handler is called and if successful will
Daniel Veillard96984452000-08-31 13:50:12 +0000125 return an I/O Input buffer</li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000126<li>the parser will the start reading from this buffer and progressively
Daniel Veillard96984452000-08-31 13:50:12 +0000127 fetch information from the resource, calling the read() function of the
128 handler until the resource is exhausted</li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000129<li>if an encoding change is detected it will be installed on the input
Daniel Veillard96984452000-08-31 13:50:12 +0000130 buffer, providing buffering and efficient use of the conversion
131 routines</li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000132<li>once the parser has finished, the close() function of the handler is
Daniel Veillard96984452000-08-31 13:50:12 +0000133 called once and the Input buffer and associed resources are
134 deallocated.</li>
135</ol>
Daniel Veillard96984452000-08-31 13:50:12 +0000136<p>The user defined callbacks are checked first to allow overriding of the
137default libxml I/O routines.</p>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000138<h3><a name="basic">The basic buffer type</a></h3>
Daniel Veillard96984452000-08-31 13:50:12 +0000139<p>All the buffer manipulation handling is done using the
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000140<code>xmlBuffer</code> type define in <code><a href="http://xmlsoft.org/html/libxml-tree.html">tree.h</a></code>which is a
141resizable memory buffer. The buffer allocation strategy can be selected to be
142either best-fit or use an exponential doubling one (CPU vs. memory use
Daniel Veillard96984452000-08-31 13:50:12 +0000143tradeoff). The values are <code>XML_BUFFER_ALLOC_EXACT</code> and
144<code>XML_BUFFER_ALLOC_DOUBLEIT</code>, and can be set individually or on a
145system wide basis using <code>xmlBufferSetAllocationScheme()</code>. A number
146of functions allows to manipulate buffers with names starting with the
147<code>xmlBuffer...</code> prefix.</p>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000148<h3><a name="Input">Input I/O handlers</a></h3>
Daniel Veillard96984452000-08-31 13:50:12 +0000149<p>An Input I/O handler is a simple structure
150<code>xmlParserInputBuffer</code> containing a context associated to the
151resource (file descriptor, or pointer to a protocol handler), the read() and
152close() callbacks to use and an xmlBuffer. And extra xmlBuffer and a charset
153encoding handler are also present to support charset conversion when
154needed.</p>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000155<h3><a name="Output">Output I/O handlers</a></h3>
Daniel Veillard96984452000-08-31 13:50:12 +0000156<p>An Output handler <code>xmlOutputBuffer</code> is completely similar to an
157Input one except the callbacks are write() and close().</p>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000158<h3><a name="entities">The entities loader</a></h3>
Daniel Veillard2bb89092000-08-31 14:57:50 +0000159<p>The entity loader resolves requests for new entities and create inputs for
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000160the parser. Creating an input from a filename or an URI string is done
161through the xmlNewInputFromFile() routine. The default entity loader do not
162handle the PUBLIC identifier associated with an entity (if any). So it just
163calls xmlNewInputFromFile() with the SYSTEM identifier (which is mandatory in
Daniel Veillard2bb89092000-08-31 14:57:50 +0000164XML).</p>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000165<p>If you want to hook up a catalog mechanism then you simply need to
166override the default entity loader, here is an example:</p>
Daniel Veillard2bb89092000-08-31 14:57:50 +0000167<pre>#include &lt;libxml/xmlIO.h&gt;
168
169xmlExternalEntityLoader defaultLoader = NULL;
170
171xmlParserInputPtr
172xmlMyExternalEntityLoader(const char *URL, const char *ID,
173 xmlParserCtxtPtr ctxt) {
174 xmlParserInputPtr ret;
175 const char *fileID = NULL;
176 /* lookup for the fileID depending on ID */
177
178 ret = xmlNewInputFromFile(ctxt, fileID);
179 if (ret != NULL)
180 return(ret);
181 if (defaultLoader != NULL)
182 ret = defaultLoader(URL, ID, ctxt);
183 return(ret);
184}
185
186int main(..) {
187 ...
188
189 /*
190 * Install our own entity loader
191 */
192 defaultLoader = xmlGetExternalEntityLoader();
193 xmlSetExternalEntityLoader(xmlMyExternalEntityLoader);
194
195 ...
196}</pre>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000197<h3><a name="Example2">Example of customized I/O</a></h3>
Daniel Veillard96984452000-08-31 13:50:12 +0000198<p>This example come from <a href="http://xmlsoft.org/messages/0708.html">a
199real use case</a>, xmlDocDump() closes the FILE * passed by the application
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000200and this was a problem. The <a href="http://xmlsoft.org/messages/0711.html">solution</a> was to redefine a
Daniel Veillard96984452000-08-31 13:50:12 +0000201new output handler with the closing call deactivated:</p>
202<ol>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000203<li>First define a new I/O ouput allocator where the output don't close the
Daniel Veillard96984452000-08-31 13:50:12 +0000204 file:
Daniel Veillarda7ad4522000-08-31 14:19:54 +0000205 <pre>xmlOutputBufferPtr
206xmlOutputBufferCreateOwn(FILE *file, xmlCharEncodingHandlerPtr encoder) {
207    xmlOutputBufferPtr ret;
208    
209    if (xmlOutputCallbackInitialized == 0)
210        xmlRegisterDefaultOutputCallbacks();
211
212    if (file == NULL) return(NULL);
213    ret = xmlAllocOutputBuffer(encoder);
214    if (ret != NULL) {
215        ret-&gt;context = file;
216        ret-&gt;writecallback = xmlFileWrite;
217        ret-&gt;closecallback = NULL; /* No close callback */
218    }
219    return(ret); <br>
Daniel Veillard2bb89092000-08-31 14:57:50 +0000220
221
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000222
Daniel Veillard9c466822001-10-25 12:03:39 +0000223
224
Daniel Veillardc6271d22001-10-27 07:50:58 +0000225
Daniel Veillard51095312001-10-28 18:51:57 +0000226
Daniel Veillard52dcab32001-10-30 12:51:17 +0000227
Daniel Veillarded421aa2001-11-04 21:22:45 +0000228
Daniel Veillard43d3f612001-11-10 11:57:23 +0000229
Daniel Veillarda4871052001-11-26 13:19:48 +0000230
Daniel Veillard1aadc442001-11-28 13:10:32 +0000231
Daniel Veillardef90ba72001-12-07 14:24:22 +0000232
Daniel Veillard9ae4b7a2001-12-13 14:24:09 +0000233
Daniel Veillard845cce42002-01-09 11:51:37 +0000234
Daniel Veillard744683d2002-01-14 17:30:20 +0000235
Daniel Veillard35e937a2002-01-19 22:21:54 +0000236
Daniel Veillard96984452000-08-31 13:50:12 +0000237} </pre>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000238</li>
239<li>And then use it to save the document:
Daniel Veillard96984452000-08-31 13:50:12 +0000240 <pre>FILE *f;
Daniel Veillarda7ad4522000-08-31 14:19:54 +0000241xmlOutputBufferPtr output;
242xmlDocPtr doc;
Daniel Veillard96984452000-08-31 13:50:12 +0000243int res;
Daniel Veillarda7ad4522000-08-31 14:19:54 +0000244
Daniel Veillard96984452000-08-31 13:50:12 +0000245f = ...
246doc = ....
Daniel Veillard96984452000-08-31 13:50:12 +0000247
Daniel Veillarda7ad4522000-08-31 14:19:54 +0000248output = xmlOutputBufferCreateOwn(f, NULL);
249res = xmlSaveFileTo(output, doc, NULL);
Daniel Veillard96984452000-08-31 13:50:12 +0000250 </pre>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000251</li>
Daniel Veillard96984452000-08-31 13:50:12 +0000252</ol>
Daniel Veillardc5d64342001-06-24 12:13:24 +0000253<p><a href="mailto:daniel@veillard.com">Daniel Veillard</a></p>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000254</td></tr></table></td></tr></table></td></tr></table></td>
255</tr></table></td></tr></table>
Daniel Veillard96984452000-08-31 13:50:12 +0000256</body>
257</html>