blob: c9bf2ff009176b70350720fcc3c2458fe332ddcc [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">
Daniel Veillardc332dab2002-03-29 14:08:27 +00005<link rel="SHORTCUT ICON" href="/favicon.ico">
Daniel Veillardb8cfbd12001-10-25 10:53:28 +00006<style type="text/css"><!--
Daniel Veillard373a4752002-02-21 14:46:29 +00007TD {font-family: Verdana,Arial,Helvetica}
8BODY {font-family: Verdana,Arial,Helvetica; margin-top: 2em; margin-left: 0em; margin-right: 0em}
9H1 {font-family: Verdana,Arial,Helvetica}
10H2 {font-family: Verdana,Arial,Helvetica}
11H3 {font-family: Verdana,Arial,Helvetica}
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000012A:link, A:visited, A:active { text-decoration: underline }
13--></style>
14<title>I/O Interfaces</title>
Daniel Veillard96984452000-08-31 13:50:12 +000015</head>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000016<body bgcolor="#8b7765" text="#000000" link="#000000" vlink="#000000">
17<table border="0" width="100%" cellpadding="5" cellspacing="0" align="center"><tr>
18<td width="180">
19<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>
20</td>
21<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">
22<h1>The XML C library for Gnome</h1>
23<h2>I/O Interfaces</h2>
24</td></tr></table></td></tr></table></td>
25</tr></table>
26<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>
27<td valign="top" width="200" bgcolor="#8b7765"><table border="0" cellspacing="0" cellpadding="1" width="100%" bgcolor="#000000"><tr><td>
28<table width="100%" border="0" cellspacing="1" cellpadding="3">
29<tr><td colspan="1" bgcolor="#eecfa1" align="center"><center><b>Main Menu</b></center></td></tr>
Daniel Veillard8acca112002-01-21 09:52:27 +000030<tr><td bgcolor="#fffacd"><ul>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000031<li><a href="index.html">Home</a></li>
32<li><a href="intro.html">Introduction</a></li>
33<li><a href="FAQ.html">FAQ</a></li>
34<li><a href="docs.html">Documentation</a></li>
35<li><a href="bugs.html">Reporting bugs and getting help</a></li>
36<li><a href="help.html">How to help</a></li>
37<li><a href="downloads.html">Downloads</a></li>
38<li><a href="news.html">News</a></li>
Daniel Veillard7b602b42002-01-08 13:26:00 +000039<li><a href="XMLinfo.html">XML</a></li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000040<li><a href="XSLT.html">XSLT</a></li>
Daniel Veillard6dbcaf82002-02-20 14:37:47 +000041<li><a href="python.html">Python and bindings</a></li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000042<li><a href="architecture.html">libxml architecture</a></li>
43<li><a href="tree.html">The tree output</a></li>
44<li><a href="interface.html">The SAX interface</a></li>
45<li><a href="xmldtd.html">Validation &amp; DTDs</a></li>
46<li><a href="xmlmem.html">Memory Management</a></li>
47<li><a href="encoding.html">Encodings support</a></li>
48<li><a href="xmlio.html">I/O Interfaces</a></li>
49<li><a href="catalog.html">Catalog support</a></li>
50<li><a href="library.html">The parser interfaces</a></li>
51<li><a href="entities.html">Entities or no entities</a></li>
52<li><a href="namespaces.html">Namespaces</a></li>
53<li><a href="upgrade.html">Upgrading 1.x code</a></li>
Daniel Veillard52dcab32001-10-30 12:51:17 +000054<li><a href="threads.html">Thread safety</a></li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000055<li><a href="DOM.html">DOM Principles</a></li>
56<li><a href="example.html">A real example</a></li>
57<li><a href="contribs.html">Contributions</a></li>
58<li>
59<a href="xml.html">flat page</a>, <a href="site.xsl">stylesheet</a>
60</li>
61</ul></td></tr>
62</table>
63<table width="100%" border="0" cellspacing="1" cellpadding="3">
Daniel Veillard3bf65be2002-01-23 12:36:34 +000064<tr><td colspan="1" bgcolor="#eecfa1" align="center"><center><b>API Indexes</b></center></td></tr>
65<tr><td bgcolor="#fffacd"><ul>
Daniel Veillardf8592562002-01-23 17:58:17 +000066<li><a href="APIchunk0.html">Alphabetic</a></li>
Daniel Veillard3bf65be2002-01-23 12:36:34 +000067<li><a href="APIconstructors.html">Constructors</a></li>
68<li><a href="APIfunctions.html">Functions/Types</a></li>
69<li><a href="APIfiles.html">Modules</a></li>
70<li><a href="APIsymbols.html">Symbols</a></li>
71</ul></td></tr>
72</table>
73<table width="100%" border="0" cellspacing="1" cellpadding="3">
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000074<tr><td colspan="1" bgcolor="#eecfa1" align="center"><center><b>Related links</b></center></td></tr>
Daniel Veillard8acca112002-01-21 09:52:27 +000075<tr><td bgcolor="#fffacd"><ul>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000076<li><a href="http://mail.gnome.org/archives/xml/">Mail archive</a></li>
77<li><a href="http://xmlsoft.org/XSLT/">XSLT libxslt</a></li>
Daniel Veillard4a859202002-01-08 11:49:22 +000078<li><a href="http://phd.cs.unibo.it/gdome2/">DOM gdome2</a></li>
Daniel Veillard2d347fa2002-03-17 10:34:11 +000079<li><a href="http://www.aleksey.com/xmlsec/">XML-DSig xmlsec</a></li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000080<li><a href="ftp://xmlsoft.org/">FTP</a></li>
81<li><a href="http://www.fh-frankfurt.de/~igor/projects/libxml/">Windows binaries</a></li>
Daniel Veillarddb9dfd92001-11-26 17:25:02 +000082<li><a href="http://garypennington.net/libxml2/">Solaris binaries</a></li>
Daniel Veillarde6d8e202002-05-02 06:11:10 +000083<li><a href="http://sourceforge.net/projects/libxml2-pas/">Pascal bindings</a></li>
Daniel Veillard2d347fa2002-03-17 10:34:11 +000084<li><a href="http://bugzilla.gnome.org/buglist.cgi?product=libxml&amp;product=libxml2">Bug Tracker</a></li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000085</ul></td></tr>
86</table>
87</td></tr></table></td>
88<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 +000089<p>Table of Content:</p>
90<ol>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000091<li><a href="#General1">General overview</a></li>
92<li><a href="#basic">The basic buffer type</a></li>
93<li><a href="#Input">Input I/O handlers</a></li>
94<li><a href="#Output">Output I/O handlers</a></li>
95<li><a href="#entities">The entities loader</a></li>
96<li><a href="#Example2">Example of customized I/O</a></li>
Daniel Veillard96984452000-08-31 13:50:12 +000097</ol>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +000098<h3><a name="General1">General overview</a></h3>
99<p>The module <code><a href="http://xmlsoft.org/html/libxml-xmlio.html">xmlIO.h</a></code> provides
100the interfaces to the libxml I/O system. This consists of 4 main parts:</p>
Daniel Veillard96984452000-08-31 13:50:12 +0000101<ul>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000102<li>Entities loader, this is a routine which tries to fetch the entities
Daniel Veillard2bb89092000-08-31 14:57:50 +0000103 (files) based on their PUBLIC and SYSTEM identifiers. The default loader
104 don't look at the public identifier since libxml do not maintain a
105 catalog. You can redefine you own entity loader by using
106 <code>xmlGetExternalEntityLoader()</code> and
Daniel Veillard9c466822001-10-25 12:03:39 +0000107 <code>xmlSetExternalEntityLoader()</code>. <a href="#entities">Check the
108 example</a>.</li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000109<li>Input I/O buffers which are a commodity structure used by the parser(s)
Daniel Veillard96984452000-08-31 13:50:12 +0000110 input layer to handle fetching the informations to feed the parser. This
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000111 provides buffering and is also a placeholder where the encoding
Daniel Veillard63d83142002-05-20 06:51:05 +0000112 converters to UTF8 are piggy-backed.</li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000113<li>Output I/O buffers are similar to the Input ones and fulfill similar
Daniel Veillard96984452000-08-31 13:50:12 +0000114 task but when generating a serialization from a tree.</li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000115<li>A mechanism to register sets of I/O callbacks and associate them with
Daniel Veillard96984452000-08-31 13:50:12 +0000116 specific naming schemes like the protocol part of the URIs.
117 <p>This affect the default I/O operations and allows to use specific I/O
118 handlers for certain names.</p>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000119</li>
Daniel Veillard96984452000-08-31 13:50:12 +0000120</ul>
Daniel Veillard2bb89092000-08-31 14:57:50 +0000121<p>The general mechanism used when loading http://rpmfind.net/xml.html for
Daniel Veillard96984452000-08-31 13:50:12 +0000122example in the HTML parser is the following:</p>
123<ol>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000124<li>The default entity loader calls <code>xmlNewInputFromFile()</code> with
Daniel Veillard2bb89092000-08-31 14:57:50 +0000125 the parsing context and the URI string.</li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000126<li>the URI string is checked against the existing registered handlers
127 using their match() callback function, if the HTTP module was compiled
128 in, it is registered and its match() function will succeeds</li>
129<li>the open() function of the handler is called and if successful will
Daniel Veillard96984452000-08-31 13:50:12 +0000130 return an I/O Input buffer</li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000131<li>the parser will the start reading from this buffer and progressively
Daniel Veillard96984452000-08-31 13:50:12 +0000132 fetch information from the resource, calling the read() function of the
133 handler until the resource is exhausted</li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000134<li>if an encoding change is detected it will be installed on the input
Daniel Veillard96984452000-08-31 13:50:12 +0000135 buffer, providing buffering and efficient use of the conversion
136 routines</li>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000137<li>once the parser has finished, the close() function of the handler is
Daniel Veillard63d83142002-05-20 06:51:05 +0000138 called once and the Input buffer and associated resources are
Daniel Veillard96984452000-08-31 13:50:12 +0000139 deallocated.</li>
140</ol>
Daniel Veillard96984452000-08-31 13:50:12 +0000141<p>The user defined callbacks are checked first to allow overriding of the
142default libxml I/O routines.</p>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000143<h3><a name="basic">The basic buffer type</a></h3>
Daniel Veillard96984452000-08-31 13:50:12 +0000144<p>All the buffer manipulation handling is done using the
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000145<code>xmlBuffer</code> type define in <code><a href="http://xmlsoft.org/html/libxml-tree.html">tree.h</a></code>which is a
146resizable memory buffer. The buffer allocation strategy can be selected to be
147either best-fit or use an exponential doubling one (CPU vs. memory use
Daniel Veillard63d83142002-05-20 06:51:05 +0000148trade-off). The values are <code>XML_BUFFER_ALLOC_EXACT</code> and
Daniel Veillard96984452000-08-31 13:50:12 +0000149<code>XML_BUFFER_ALLOC_DOUBLEIT</code>, and can be set individually or on a
150system wide basis using <code>xmlBufferSetAllocationScheme()</code>. A number
151of functions allows to manipulate buffers with names starting with the
152<code>xmlBuffer...</code> prefix.</p>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000153<h3><a name="Input">Input I/O handlers</a></h3>
Daniel Veillard96984452000-08-31 13:50:12 +0000154<p>An Input I/O handler is a simple structure
155<code>xmlParserInputBuffer</code> containing a context associated to the
156resource (file descriptor, or pointer to a protocol handler), the read() and
157close() callbacks to use and an xmlBuffer. And extra xmlBuffer and a charset
158encoding handler are also present to support charset conversion when
159needed.</p>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000160<h3><a name="Output">Output I/O handlers</a></h3>
Daniel Veillard96984452000-08-31 13:50:12 +0000161<p>An Output handler <code>xmlOutputBuffer</code> is completely similar to an
162Input one except the callbacks are write() and close().</p>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000163<h3><a name="entities">The entities loader</a></h3>
Daniel Veillard2bb89092000-08-31 14:57:50 +0000164<p>The entity loader resolves requests for new entities and create inputs for
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000165the parser. Creating an input from a filename or an URI string is done
166through the xmlNewInputFromFile() routine. The default entity loader do not
167handle the PUBLIC identifier associated with an entity (if any). So it just
168calls xmlNewInputFromFile() with the SYSTEM identifier (which is mandatory in
Daniel Veillard2bb89092000-08-31 14:57:50 +0000169XML).</p>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000170<p>If you want to hook up a catalog mechanism then you simply need to
171override the default entity loader, here is an example:</p>
Daniel Veillard2bb89092000-08-31 14:57:50 +0000172<pre>#include &lt;libxml/xmlIO.h&gt;
173
174xmlExternalEntityLoader defaultLoader = NULL;
175
176xmlParserInputPtr
177xmlMyExternalEntityLoader(const char *URL, const char *ID,
178 xmlParserCtxtPtr ctxt) {
179 xmlParserInputPtr ret;
180 const char *fileID = NULL;
181 /* lookup for the fileID depending on ID */
182
183 ret = xmlNewInputFromFile(ctxt, fileID);
184 if (ret != NULL)
185 return(ret);
186 if (defaultLoader != NULL)
187 ret = defaultLoader(URL, ID, ctxt);
188 return(ret);
189}
190
191int main(..) {
192 ...
193
194 /*
195 * Install our own entity loader
196 */
197 defaultLoader = xmlGetExternalEntityLoader();
198 xmlSetExternalEntityLoader(xmlMyExternalEntityLoader);
199
200 ...
201}</pre>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000202<h3><a name="Example2">Example of customized I/O</a></h3>
Daniel Veillard96984452000-08-31 13:50:12 +0000203<p>This example come from <a href="http://xmlsoft.org/messages/0708.html">a
204real use case</a>, xmlDocDump() closes the FILE * passed by the application
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000205and 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 +0000206new output handler with the closing call deactivated:</p>
207<ol>
Daniel Veillardc0801af2002-05-28 16:28:42 +0000208<li>First define a new I/O output allocator where the output don't close
209 the file:
Daniel Veillarda7ad4522000-08-31 14:19:54 +0000210 <pre>xmlOutputBufferPtr
211xmlOutputBufferCreateOwn(FILE *file, xmlCharEncodingHandlerPtr encoder) {
212    xmlOutputBufferPtr ret;
213    
214    if (xmlOutputCallbackInitialized == 0)
215        xmlRegisterDefaultOutputCallbacks();
216
217    if (file == NULL) return(NULL);
218    ret = xmlAllocOutputBuffer(encoder);
219    if (ret != NULL) {
220        ret-&gt;context = file;
221        ret-&gt;writecallback = xmlFileWrite;
222        ret-&gt;closecallback = NULL; /* No close callback */
223    }
224    return(ret); <br>
Daniel Veillard1eb24242002-03-18 11:33:03 +0000225
Daniel Veillard34ce8be2002-03-18 19:37:11 +0000226
227
Daniel Veillard19274092002-03-25 16:48:03 +0000228
Daniel Veillardb2fb8ed2002-04-01 09:33:12 +0000229
Daniel Veillarda8a89fe2002-04-12 21:03:34 +0000230
Daniel Veillardc3975342002-04-15 18:54:52 +0000231
Daniel Veillard0bfbb422002-04-26 09:21:45 +0000232
Daniel Veillard93d3a472002-04-26 14:04:55 +0000233
Daniel Veillardcf27f7c2002-04-30 07:12:39 +0000234
Daniel Veillarde6d8e202002-05-02 06:11:10 +0000235
Daniel Veillard9b6fd302002-05-13 12:06:47 +0000236
Daniel Veillardc0801af2002-05-28 16:28:42 +0000237
Daniel Veillard96984452000-08-31 13:50:12 +0000238} </pre>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000239</li>
240<li>And then use it to save the document:
Daniel Veillard96984452000-08-31 13:50:12 +0000241 <pre>FILE *f;
Daniel Veillarda7ad4522000-08-31 14:19:54 +0000242xmlOutputBufferPtr output;
243xmlDocPtr doc;
Daniel Veillard96984452000-08-31 13:50:12 +0000244int res;
Daniel Veillarda7ad4522000-08-31 14:19:54 +0000245
Daniel Veillard96984452000-08-31 13:50:12 +0000246f = ...
247doc = ....
Daniel Veillard96984452000-08-31 13:50:12 +0000248
Daniel Veillarda7ad4522000-08-31 14:19:54 +0000249output = xmlOutputBufferCreateOwn(f, NULL);
250res = xmlSaveFileTo(output, doc, NULL);
Daniel Veillard96984452000-08-31 13:50:12 +0000251 </pre>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000252</li>
Daniel Veillard96984452000-08-31 13:50:12 +0000253</ol>
Daniel Veillard3f4c40f2002-02-13 09:19:28 +0000254<p><a href="bugs.html">Daniel Veillard</a></p>
Daniel Veillardb8cfbd12001-10-25 10:53:28 +0000255</td></tr></table></td></tr></table></td></tr></table></td>
256</tr></table></td></tr></table>
Daniel Veillard96984452000-08-31 13:50:12 +0000257</body>
258</html>