xmlio.html revision a539356ae05a462a04a2e42ddb28f8f8b71bf31c
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
2<html>
3<head>
4<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
5<style type="text/css"><!--
6TD {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}
11A:link, A:visited, A:active { text-decoration: underline }
12--></style>
13<title>I/O Interfaces</title>
14</head>
15<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>
29<tr><td bgcolor="#fffacd"><ul>
30<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>
38<li><a href="XMLinfo.html">XML</a></li>
39<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>
52<li><a href="threads.html">Thread safety</a></li>
53<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">
62<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="APIchunk0.html">Alphabetic</a></li>
65<li><a href="APIconstructors.html">Constructors</a></li>
66<li><a href="APIfunctions.html">Functions/Types</a></li>
67<li><a href="APIfiles.html">Modules</a></li>
68<li><a href="APIsymbols.html">Symbols</a></li>
69</ul></td></tr>
70</table>
71<table width="100%" border="0" cellspacing="1" cellpadding="3">
72<tr><td colspan="1" bgcolor="#eecfa1" align="center"><center><b>Related links</b></center></td></tr>
73<tr><td bgcolor="#fffacd"><ul>
74<li><a href="http://mail.gnome.org/archives/xml/">Mail archive</a></li>
75<li><a href="http://xmlsoft.org/XSLT/">XSLT libxslt</a></li>
76<li><a href="http://phd.cs.unibo.it/gdome2/">DOM gdome2</a></li>
77<li><a href="ftp://xmlsoft.org/">FTP</a></li>
78<li><a href="http://www.fh-frankfurt.de/~igor/projects/libxml/">Windows binaries</a></li>
79<li><a href="http://garypennington.net/libxml2/">Solaris binaries</a></li>
80<li><a href="http://bugzilla.gnome.org/buglist.cgi?product=libxml">Bug Tracker</a></li>
81</ul></td></tr>
82</table>
83</td></tr></table></td>
84<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">
85<p>Table of Content:</p>
86<ol>
87<li><a href="#General1">General overview</a></li>
88<li><a href="#basic">The basic buffer type</a></li>
89<li><a href="#Input">Input I/O handlers</a></li>
90<li><a href="#Output">Output I/O handlers</a></li>
91<li><a href="#entities">The entities loader</a></li>
92<li><a href="#Example2">Example of customized I/O</a></li>
93</ol>
94<h3><a name="General1">General overview</a></h3>
95<p>The module <code><a href="http://xmlsoft.org/html/libxml-xmlio.html">xmlIO.h</a></code> provides
96the interfaces to the libxml I/O system. This consists of 4 main parts:</p>
97<ul>
98<li>Entities loader, this is a routine which tries to fetch the entities
99    (files) based on their PUBLIC and SYSTEM identifiers. The default loader
100    don't look at the public identifier since libxml do not maintain a
101    catalog. You can redefine you own entity loader by using
102    <code>xmlGetExternalEntityLoader()</code> and
103    <code>xmlSetExternalEntityLoader()</code>. <a href="#entities">Check the
104    example</a>.</li>
105<li>Input I/O buffers which are a commodity structure used by the parser(s)
106    input layer to handle fetching the informations to feed the parser. This
107    provides buffering and is also a placeholder where the encoding
108    convertors to UTF8 are piggy-backed.</li>
109<li>Output I/O buffers are similar to the Input ones and fulfill similar
110    task but when generating a serialization from a tree.</li>
111<li>A mechanism to register sets of I/O callbacks and associate them with
112    specific naming schemes like the protocol part of the URIs.
113    <p>This affect the default I/O operations and allows to use specific I/O
114    handlers for certain names.</p>
115</li>
116</ul>
117<p>The general mechanism used when loading http://rpmfind.net/xml.html for
118example in the HTML parser is the following:</p>
119<ol>
120<li>The default entity loader calls <code>xmlNewInputFromFile()</code> with
121    the parsing context and the URI string.</li>
122<li>the URI string is checked against the existing registered handlers
123    using their match() callback function, if the HTTP module was compiled
124    in, it is registered and its match() function will succeeds</li>
125<li>the open() function of the handler is called and if successful will
126    return an I/O Input buffer</li>
127<li>the parser will the start reading from this buffer and progressively
128    fetch information from the resource, calling the read() function of the
129    handler until the resource is exhausted</li>
130<li>if an encoding change is detected it will be installed on the input
131    buffer, providing buffering and efficient use of the conversion
132  routines</li>
133<li>once the parser has finished, the close() function of the handler is
134    called once and the Input buffer and associed resources are
135  deallocated.</li>
136</ol>
137<p>The user defined callbacks are checked first to allow overriding of the
138default libxml I/O routines.</p>
139<h3><a name="basic">The basic buffer type</a></h3>
140<p>All the buffer manipulation handling is done using the
141<code>xmlBuffer</code> type define in <code><a href="http://xmlsoft.org/html/libxml-tree.html">tree.h</a></code>which is a
142resizable memory buffer. The buffer allocation strategy can be selected to be
143either best-fit or use an exponential doubling one (CPU vs. memory use
144tradeoff). The values are <code>XML_BUFFER_ALLOC_EXACT</code> and
145<code>XML_BUFFER_ALLOC_DOUBLEIT</code>, and can be set individually or on a
146system wide basis using <code>xmlBufferSetAllocationScheme()</code>. A number
147of functions allows to manipulate buffers with names starting with the
148<code>xmlBuffer...</code> prefix.</p>
149<h3><a name="Input">Input I/O handlers</a></h3>
150<p>An Input I/O handler is a simple structure
151<code>xmlParserInputBuffer</code> containing a context associated to the
152resource (file descriptor, or pointer to a protocol handler), the read() and
153close() callbacks to use and an xmlBuffer. And extra xmlBuffer and a charset
154encoding handler are also present to support charset conversion when
155needed.</p>
156<h3><a name="Output">Output I/O handlers</a></h3>
157<p>An Output handler <code>xmlOutputBuffer</code> is completely similar to an
158Input one except the callbacks are write() and close().</p>
159<h3><a name="entities">The entities loader</a></h3>
160<p>The entity loader resolves requests for new entities and create inputs for
161the parser. Creating an input from a filename or an URI string is done
162through the xmlNewInputFromFile() routine.  The default entity loader do not
163handle the PUBLIC identifier associated with an entity (if any). So it just
164calls xmlNewInputFromFile() with the SYSTEM identifier (which is mandatory in
165XML).</p>
166<p>If you want to hook up a catalog mechanism then you simply need to
167override the default entity loader, here is an example:</p>
168<pre>#include &lt;libxml/xmlIO.h&gt;
169
170xmlExternalEntityLoader defaultLoader = NULL;
171
172xmlParserInputPtr
173xmlMyExternalEntityLoader(const char *URL, const char *ID,
174                               xmlParserCtxtPtr ctxt) {
175    xmlParserInputPtr ret;
176    const char *fileID = NULL;
177    /* lookup for the fileID depending on ID */
178
179    ret = xmlNewInputFromFile(ctxt, fileID);
180    if (ret != NULL)
181        return(ret);
182    if (defaultLoader != NULL)
183        ret = defaultLoader(URL, ID, ctxt);
184    return(ret);
185}
186
187int main(..) {
188    ...
189
190    /*
191     * Install our own entity loader
192     */
193    defaultLoader = xmlGetExternalEntityLoader();
194    xmlSetExternalEntityLoader(xmlMyExternalEntityLoader);
195
196    ...
197}</pre>
198<h3><a name="Example2">Example of customized I/O</a></h3>
199<p>This example come from <a href="http://xmlsoft.org/messages/0708.html">a
200real use case</a>,  xmlDocDump() closes the FILE * passed by the application
201and this was a problem. The <a href="http://xmlsoft.org/messages/0711.html">solution</a> was to redefine a
202new output handler with the closing call deactivated:</p>
203<ol>
204<li>First define a new I/O ouput allocator where the output don't close the
205    file:
206    <pre>xmlOutputBufferPtr
207xmlOutputBufferCreateOwn(FILE *file, xmlCharEncodingHandlerPtr encoder) {
208����xmlOutputBufferPtr ret;
209����
210����if (xmlOutputCallbackInitialized == 0)
211��������xmlRegisterDefaultOutputCallbacks();
212
213����if (file == NULL) return(NULL);
214����ret = xmlAllocOutputBuffer(encoder);
215����if (ret != NULL) {
216��������ret-&gt;context = file;
217��������ret-&gt;writecallback = xmlFileWrite;
218��������ret-&gt;closecallback = NULL;  /* No close callback */
219����}
220����return(ret); <br>
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245} </pre>
246</li>
247<li>And then use it to save the document:
248    <pre>FILE *f;
249xmlOutputBufferPtr output;
250xmlDocPtr doc;
251int res;
252
253f = ...
254doc = ....
255
256output = xmlOutputBufferCreateOwn(f, NULL);
257res = xmlSaveFileTo(output, doc, NULL);
258    </pre>
259</li>
260</ol>
261<p><a href="bugs.html">Daniel Veillard</a></p>
262</td></tr></table></td></tr></table></td></tr></table></td>
263</tr></table></td></tr></table>
264</body>
265</html>
266