Daniel Veillard | 9698445 | 2000-08-31 13:50:12 +0000 | [diff] [blame] | 1 | <!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 |
| 14 | href="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 |
| 19 | href="http://xmlsoft.org/messages/">http://xmlsoft.org/messages/</a></p> |
| 20 | |
Daniel Veillard | 2bb8909 | 2000-08-31 14:57:50 +0000 | [diff] [blame^] | 21 | <p>Version: $Revision: 1.2 $</p> |
Daniel Veillard | 9698445 | 2000-08-31 13:50:12 +0000 | [diff] [blame] | 22 | |
| 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 Veillard | 2bb8909 | 2000-08-31 14:57:50 +0000 | [diff] [blame^] | 29 | <li><a href="#entities">The entities loader</a></li> |
Daniel Veillard | 9698445 | 2000-08-31 13:50:12 +0000 | [diff] [blame] | 30 | <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 |
| 36 | href="http://xmlsoft.org/html/gnome-xml-xmlio.html">xmlIO.h</a></code> |
Daniel Veillard | 2bb8909 | 2000-08-31 14:57:50 +0000 | [diff] [blame^] | 37 | provides the interfaces to the libxml I/O system. This consists of 4 main |
Daniel Veillard | 9698445 | 2000-08-31 13:50:12 +0000 | [diff] [blame] | 38 | parts:</p> |
| 39 | <ul> |
Daniel Veillard | 2bb8909 | 2000-08-31 14:57:50 +0000 | [diff] [blame^] | 40 | <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 Veillard | 9698445 | 2000-08-31 13:50:12 +0000 | [diff] [blame] | 47 | <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 Veillard | 2bb8909 | 2000-08-31 14:57:50 +0000 | [diff] [blame^] | 60 | <p>The general mechanism used when loading http://rpmfind.net/xml.html for |
Daniel Veillard | 9698445 | 2000-08-31 13:50:12 +0000 | [diff] [blame] | 61 | example in the HTML parser is the following:</p> |
| 62 | <ol> |
Daniel Veillard | 2bb8909 | 2000-08-31 14:57:50 +0000 | [diff] [blame^] | 63 | <li>The default entity loader calls <code>xmlNewInputFromFile()</code> with |
| 64 | the parsing context and the URI string.</li> |
Daniel Veillard | 9698445 | 2000-08-31 13:50:12 +0000 | [diff] [blame] | 65 | <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 Veillard | 2bb8909 | 2000-08-31 14:57:50 +0000 | [diff] [blame^] | 67 | registered and its match() function will succeeds</li> |
Daniel Veillard | 9698445 | 2000-08-31 13:50:12 +0000 | [diff] [blame] | 68 | <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 |
| 82 | default 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 |
| 88 | href="http://xmlsoft.org/html/gnome-xml-tree.html">tree.h</a> </code>which is |
| 89 | a resizable memory buffer. The buffer allocation strategy can be selected to |
| 90 | be either best-fit or use an exponential doubling one (CPU vs. memory use |
| 91 | tradeoff). 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 |
| 93 | system wide basis using <code>xmlBufferSetAllocationScheme()</code>. A number |
| 94 | of 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 |
| 101 | resource (file descriptor, or pointer to a protocol handler), the read() and |
| 102 | close() callbacks to use and an xmlBuffer. And extra xmlBuffer and a charset |
| 103 | encoding handler are also present to support charset conversion when |
| 104 | needed.</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 |
| 109 | Input one except the callbacks are write() and close().</p> |
| 110 | |
Daniel Veillard | 2bb8909 | 2000-08-31 14:57:50 +0000 | [diff] [blame^] | 111 | <h2><a name="entities">The entities loader</a></h2> |
| 112 | |
| 113 | <p>The entity loader resolves requests for new entities and create inputs for |
| 114 | the parser. Creating an input from a filename or an URI string is done through |
| 115 | the xmlNewInputFromFile() routine. The default entity loader do not handle |
| 116 | the PUBLIC identifier associated with an entity (if any). So it just calls |
| 117 | xmlNewInputFromFile() with the SYSTEM identifier (which is mandatory in |
| 118 | XML).</p> |
| 119 | |
| 120 | <p>If you want to hook up a catalog mechanism then you simply need to override |
| 121 | the default entity loader, here is an example:</p> |
| 122 | <pre>#include <libxml/xmlIO.h> |
| 123 | |
| 124 | xmlExternalEntityLoader defaultLoader = NULL; |
| 125 | |
| 126 | xmlParserInputPtr |
| 127 | xmlMyExternalEntityLoader(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 | |
| 141 | int main(..) { |
| 142 | ... |
| 143 | |
| 144 | /* |
| 145 | * Install our own entity loader |
| 146 | */ |
| 147 | defaultLoader = xmlGetExternalEntityLoader(); |
| 148 | xmlSetExternalEntityLoader(xmlMyExternalEntityLoader); |
| 149 | |
| 150 | ... |
| 151 | }</pre> |
| 152 | |
Daniel Veillard | 9698445 | 2000-08-31 13:50:12 +0000 | [diff] [blame] | 153 | <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 |
| 156 | real use case</a>, xmlDocDump() closes the FILE * passed by the application |
| 157 | and this was a problem. The <a |
| 158 | href="http://xmlsoft.org/messages/0711.html">solution</a> was to redefine a |
| 159 | new 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 Veillard | a7ad452 | 2000-08-31 14:19:54 +0000 | [diff] [blame] | 163 | <pre>xmlOutputBufferPtr |
| 164 | xmlOutputBufferCreateOwn(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->context = file; |
| 174 | ret->writecallback = xmlFileWrite; |
| 175 | ret->closecallback = NULL; /* No close callback */ |
| 176 | } |
| 177 | return(ret); <br> |
Daniel Veillard | 2bb8909 | 2000-08-31 14:57:50 +0000 | [diff] [blame^] | 178 | |
| 179 | |
Daniel Veillard | 9698445 | 2000-08-31 13:50:12 +0000 | [diff] [blame] | 180 | } </pre> |
| 181 | </li> |
| 182 | <li>And then use it to save the document: |
| 183 | <pre>FILE *f; |
Daniel Veillard | a7ad452 | 2000-08-31 14:19:54 +0000 | [diff] [blame] | 184 | xmlOutputBufferPtr output; |
| 185 | xmlDocPtr doc; |
Daniel Veillard | 9698445 | 2000-08-31 13:50:12 +0000 | [diff] [blame] | 186 | int res; |
Daniel Veillard | a7ad452 | 2000-08-31 14:19:54 +0000 | [diff] [blame] | 187 | |
Daniel Veillard | 9698445 | 2000-08-31 13:50:12 +0000 | [diff] [blame] | 188 | f = ... |
| 189 | doc = .... |
Daniel Veillard | 9698445 | 2000-08-31 13:50:12 +0000 | [diff] [blame] | 190 | |
Daniel Veillard | a7ad452 | 2000-08-31 14:19:54 +0000 | [diff] [blame] | 191 | output = xmlOutputBufferCreateOwn(f, NULL); |
| 192 | res = xmlSaveFileTo(output, doc, NULL); |
Daniel Veillard | 9698445 | 2000-08-31 13:50:12 +0000 | [diff] [blame] | 193 | </pre> |
| 194 | </li> |
| 195 | </ol> |
| 196 | |
| 197 | <p><a href="mailto:Daniel.Veillard@w3.org">Daniel Veillard</a></p> |
| 198 | |
Daniel Veillard | 2bb8909 | 2000-08-31 14:57:50 +0000 | [diff] [blame^] | 199 | <p>$Id: xmlio.html,v 1.2 2000/08/31 14:19:54 veillard Exp $</p> |
Daniel Veillard | 9698445 | 2000-08-31 13:50:12 +0000 | [diff] [blame] | 200 | </body> |
| 201 | </html> |