blob: e212a1f07e09b152ee3182e563382888ff66e613 [file] [log] [blame]
Daniel Veillardccb09631998-10-27 06:21:04 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
Daniel Veillardb05deb71999-08-10 19:04:08 +00002 "http://www.w3.org/TR/REC-html40/loose.dtd">
Daniel Veillardccb09631998-10-27 06:21:04 +00003<html>
4<head>
Daniel Veillardb05deb71999-08-10 19:04:08 +00005 <title>The XML library for Gnome</title>
6 <meta name="GENERATOR" content="amaya V2.1">
Daniel Veillardccb09631998-10-27 06:21:04 +00007</head>
Daniel Veillardccb09631998-10-27 06:21:04 +00008
Daniel Veillardb05deb71999-08-10 19:04:08 +00009<body bgcolor="#ffffff">
Daniel Veillardccb09631998-10-27 06:21:04 +000010<h1 align="center">The XML library for Gnome</h1>
Daniel Veillardb05deb71999-08-10 19:04:08 +000011
Daniel Veillardc8eab3a1999-09-04 18:27:23 +000012<h2 style="text-align: center">libxml, a.k.a. gnome-xml</h2>
13
14<p></p>
Daniel Veillard2f4dfc41999-09-24 14:03:48 +000015<ul>
16 <li><a href="#Introducti">Introduction</a></li>
17 <li><a href="#Documentat">Documentation</a></li>
18 <li><a href="#News">News</a></li>
19 <li><a href="#XML">XML</a></li>
20 <li><a href="#tree">The tree output</a></li>
21 <li><a href="#interface">The SAX interface</a></li>
22 <li><a href="#library">The XML library interfaces</a>
23 <ul>
24 <li><a href="#Invoking">Invoking the parser</a></li>
25 <li><a href="#Building">Building a tree from scratch</a></li>
26 <li><a href="#Traversing">Traversing the tree</a></li>
27 <li><a href="#Modifying">Modifying the tree</a></li>
28 <li><a href="#Saving">Saving the tree</a></li>
29 <li><a href="#Compressio">Compression</a></li>
30 </ul>
31 </li>
32 <li><a href="#Entities">Entities or no entities</a></li>
33 <li><a href="#Namespaces">Namespaces</a></li>
34 <li><a href="#Validation">Validation</a></li>
35 <li><a href="#Principles">DOM principles</a></li>
36 <li><a href="#real">A real example</a></li>
37</ul>
38
39<h2><a name="Introducti">Introduction</a></h2>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +000040
Daniel Veillardb05deb71999-08-10 19:04:08 +000041<p>This document describes the <a href="http://www.w3.org/XML/">XML</a>
42library provideed in the <a href="http://www.gnome.org/">Gnome</a> framework.
Daniel Veillardc8eab3a1999-09-04 18:27:23 +000043XML is a standard to build tag based structured documents/data.</p>
Daniel Veillardb05deb71999-08-10 19:04:08 +000044
45<p>The internal document repesentation is as close as possible to the <a
Daniel Veillardc8eab3a1999-09-04 18:27:23 +000046href="http://www.w3.org/DOM/">DOM</a> interfaces.</p>
Daniel Veillardb05deb71999-08-10 19:04:08 +000047
48<p>Libxml also has a <a href="http://www.megginson.com/SAX/index.html">SAX
49interface</a>, <a href="mailto:james@daa.com.au">James Henstridge</a> made <a
50href="http://www.daa.com.au/~james/gnome/xml-sax/xml-sax.html">a nice
51documentation</a> expaining how to use it. The interface is as compatible as
52possible with <a href="http://www.jclark.com/xml/expat.html">Expat</a>
53one.</p>
54
Daniel Veillardb05deb71999-08-10 19:04:08 +000055<p>There is also a mailing-list <a
Daniel Veillard6bd26dc1999-09-03 14:28:40 +000056href="mailto:xml@rufus.w3.org">xml@rufus.w3.org</a> for libxml, with an <a
Daniel Veillardb05deb71999-08-10 19:04:08 +000057href="http://rpmfind.net/veillard/XML/messages">on-line archive</a>. To
58subscribe to this majordomo based list, send a mail to <a
Daniel Veillard6bd26dc1999-09-03 14:28:40 +000059href="mailto:majordomo@rufus.w3.org">majordomo@rufus.w3.org</a> with
60"subscribe xml" in the <strong>content</strong> of the message.</p>
Daniel Veillardb05deb71999-08-10 19:04:08 +000061
62<p>This library is released both under the W3C Copyright and the GNU LGP,
63basically everybody should be happy, if not, drop me a mail.</p>
64
65<p>People are invited to use the <a
66href="http://cvs.gnome.org/lxr/source/gdome/">gdome Gnome module to</a> get a
67full DOM interface, thanks to <a href="mailto:raph@levien.com">Raph
68Levien</a>, check his <a
69href="http://www.levien.com/gnome/domination.html">DOMination paper</a>. He
70uses it for his implementation of <a
71href="http://www.w3.org/Graphics/SVG/">SVG</a> called <a
72href="http://www.levien.com/svg/">gill</a>.</p>
Daniel Veillardccb09631998-10-27 06:21:04 +000073
Daniel Veillard2f4dfc41999-09-24 14:03:48 +000074<h2><a name="Documentat">Documentation</a></h2>
Daniel Veillardb05deb71999-08-10 19:04:08 +000075
Daniel Veillardc8eab3a1999-09-04 18:27:23 +000076<p>The code is commented in a <a href=""></a>way which allow <a
77href="http://rpmfind.net/veillard/XML/libxml.html">extensive documentation</a>
78to be automatically extracted.</p>
79
80<p>At some point I will change the back-end to produce XML documentation in
81addition to SGML Docbook and HTML.</p>
82
Daniel Veillard2f4dfc41999-09-24 14:03:48 +000083<h2><a name="News">News</a></h2>
84
Daniel Veillard35008381999-10-25 13:15:52 +000085<p>Latest version is 1.7.3, you can find it on <a
Daniel Veillard2f4dfc41999-09-24 14:03:48 +000086href="ftp://rpmfind.net/pub/veillard/">rpmfind.net</a> or on the <a
87href="ftp://ftp.gnome.org/pub/GNOME/MIRRORS.html">Gnome FTP server</a> either
88as a <a href="ftp://ftp.gnome.org/pub/GNOME/sources/libxml/">source
89archive</a> or <a href="ftp://ftp.gnome.org/pub/GNOME/contrib/rpms/">RPMs
90packages</a>.</p>
91
92<h3>CVS only</h3>
Daniel Veillard35008381999-10-25 13:15:52 +000093
94<h3>1.7.4: Oct 25 1999</h3>
Daniel Veillard2f4dfc41999-09-24 14:03:48 +000095<ul>
Daniel Veillard35008381999-10-25 13:15:52 +000096 <li>Lots of HTML improvement</li>
97 <li>Fixed some errors when saving both XML and HTML</li>
98 <li>More examples, the regression tests should now look clean</li>
99 <li>Fixed a bug with contiguous charref</li>
100</ul>
101
102<h3>1.7.3: Sep 29 1999</h3>
103<ul>
104 <li>portability problems fixed</li>
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000105 <li>snprintf was used unconditionnally, leading to link problems on system
Daniel Veillard35008381999-10-25 13:15:52 +0000106 were it's not available, fixed</li>
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000107</ul>
108
109<h3>1.7.1: Sep 24 1999</h3>
110<ul>
111 <li>The basic type for strings manipulated by libxml has been renamed in
112 1.7.1 from <strong>CHAR</strong> to <strong>xmlChar</strong>. The reason
113 is that CHAR was conflicting with a predefined type on Windows. However on
114 non WIN32 environment, compatibility is provided by the way of a
115 <strong>#define </strong>.</li>
116 <li>Changed another error : the use of a structure field called errno, and
117 leading to troubles on platforms where it's a macro</li>
118</ul>
119
120<h3>1.7.0: sep 23 1999</h3>
121<ul>
122 <li>Added the ability to fetch remote DTD or parsed entities, see the <a
123 href="gnome-xml-nanohttp.html">nanohttp</a> module.</li>
124 <li>Added an errno to report errors by another mean than a simple printf
125 like callback</li>
126 <li>Finished ID/IDREF support and checking when validation</li>
127 <li>Serious memory leaks fixed (there is now a <a
128 href="gnome-xml-xmlmemory.html">memory wrapper</a> module)</li>
129 <li>Improvement of <a href="http://www.w3.org/TR/xpath">XPath</a>
130 implementation</li>
131 <li>Added an HTML parser front-end</li>
132</ul>
133
134<h2><a name="XML">XML</a></h2>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000135
136<p><a href="http://www.w3.org/TR/REC-xml">XML is a standard</a> for markup
137based structured documents, here is <a name="example">an example</a>:</p>
Daniel Veillardccb09631998-10-27 06:21:04 +0000138<pre>&lt;?xml version="1.0"?>
Daniel Veillard14fff061999-06-22 21:49:07 +0000139&lt;EXAMPLE prop1="gnome is great" prop2="&amp;amp; linux too">
Daniel Veillardccb09631998-10-27 06:21:04 +0000140 &lt;head>
141 &lt;title>Welcome to Gnome&lt;/title>
142 &lt;/head>
143 &lt;chapter>
144 &lt;title>The Linux adventure&lt;/title>
145 &lt;p>bla bla bla ...&lt;/p>
146 &lt;image href="linus.gif"/>
147 &lt;p>...&lt;/p>
148 &lt;/chapter>
149&lt;/EXAMPLE></pre>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000150
151<p>The first line specify that it's an XML document and gives useful
152informations about it's encoding. Then the document is a text format whose
153structure is specified by tags between brackets. <strong>Each tag opened have
154to be closed</strong> XML is pedantic about this, not that for example the
155image tag has no content (just an attribute) and is closed by ending up the
156tag with <code>/></code>.</p>
Daniel Veillardccb09631998-10-27 06:21:04 +0000157
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000158<p>XML can be applied sucessfully to a wide range or usage from long term
159structured document maintenance where it follows the steps of SGML to simple
160data encoding mechanism like configuration file format (glade), spreadsheets
161(gnumeric), or even shorter lived document like in WebDAV where it is used to
162encode remote call between a client and a server.</p>
163
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000164<h2><a name="tree">The tree output</a></h2>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000165
166<p>The parser returns a tree built during the document analysis. The value
Daniel Veillardccb09631998-10-27 06:21:04 +0000167returned is an <strong>xmlDocPtr</strong> (i.e. a pointer to an
168<strong>xmlDoc</strong> structure). This structure contains informations like
169the file name, the document type, and a <strong>root</strong> pointer which
170is the root of the document (or more exactly the first child under the root
171which is the document). The tree is made of <strong>xmlNode</strong>s, chained
172in double linked lists of siblings and with childs&lt;->parent relationship.
173An xmlNode can also carry properties (a chain of xmlAttr structures). An
174attribute may have a value which is a list of TEXT or ENTITY_REF nodes.</p>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000175
176<p>Here is an example (erroneous w.r.t. the XML spec since there should be
177only one ELEMENT under the root):</p>
178
179<p><img src="structure.gif" alt=" structure.gif "></p>
180
181<p>In the source package there is a small program (not installed by default)
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000182called <strong>tester</strong> which parses XML files given as argument and
183prints them back as parsed, this is useful to detect errors both in XML code
184and in the XML parser itself. It has an option <strong>--debug</strong> which
185prints the actual in-memory structure of the document, here is the result with
186the <a href="#example">example</a> given before:</p>
187<pre>DOCUMENT
188version=1.0
189standalone=true
190 ELEMENT EXAMPLE
191 ATTRIBUTE prop1
192 TEXT
193 content=gnome is great
194 ATTRIBUTE prop2
195 ENTITY_REF
196 TEXT
197 content= too
198 ELEMENT head
199 ELEMENT title
Daniel Veillard25940b71998-10-29 05:51:30 +0000200 TEXT
201 content=Welcome to Gnome
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000202 ELEMENT chapter
203 ELEMENT title
Daniel Veillard25940b71998-10-29 05:51:30 +0000204 TEXT
205 content=The Linux adventure
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000206 ELEMENT p
Daniel Veillard25940b71998-10-29 05:51:30 +0000207 TEXT
208 content=bla bla bla ...
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000209 ELEMENT image
210 ATTRIBUTE href
211 TEXT
212 content=linus.gif
213 ELEMENT p
Daniel Veillard25940b71998-10-29 05:51:30 +0000214 TEXT
215 content=...</pre>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000216
217<p>This should be useful to learn the internal representation model.</p>
Daniel Veillardccb09631998-10-27 06:21:04 +0000218
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000219<h2><a name="interface">The SAX interface</a></h2>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000220
221<p>Sometimes the DOM tree output is just to large to fit reasonably into
222memory. In that case and if you don't expect to save back the XML document
223loaded using libxml, it's better to use the SAX interface of libxml. SAX is a
224<strong>callback based interface</strong> to the parser. Before parsing, the
225application layer register a customized set of callbacks which will be called
226by the library as it progresses through the XML input.</p>
227
228<p>To get a more detailed step-by-step guidance on using the SAX interface of
229libxml, <a href="mailto:james@daa.com.au">James Henstridge</a> made <a
230href="http://www.daa.com.au/~james/gnome/xml-sax/xml-sax.html">a nice
231documentation.</a></p>
232
233<p>You can debug the SAX behaviour by using the <strong>testSAX</strong>
234program located in the gnome-xml module (it's usually not shipped in the
235binary packages of libxml, but you can also find it in the tar source
236distribution). Here is the sequence of callback that would be generated when
237parsing the example given before as reported by testSAX:</p>
238<pre>SAX.setDocumentLocator()
239SAX.startDocument()
240SAX.getEntity(amp)
241SAX.startElement(EXAMPLE, prop1='gnome is great', prop2='&amp;amp; linux too')
242SAX.characters( , 3)
243SAX.startElement(head)
244SAX.characters( , 4)
245SAX.startElement(title)
246SAX.characters(Welcome to Gnome, 16)
247SAX.endElement(title)
248SAX.characters( , 3)
249SAX.endElement(head)
250SAX.characters( , 3)
251SAX.startElement(chapter)
252SAX.characters( , 4)
253SAX.startElement(title)
254SAX.characters(The Linux adventure, 19)
255SAX.endElement(title)
256SAX.characters( , 4)
257SAX.startElement(p)
258SAX.characters(bla bla bla ..., 15)
259SAX.endElement(p)
260SAX.characters( , 4)
261SAX.startElement(image, href='linus.gif')
262SAX.endElement(image)
263SAX.characters( , 4)
264SAX.startElement(p)
265SAX.characters(..., 3)
266SAX.endElement(p)
267SAX.characters( , 3)
268SAX.endElement(chapter)
269SAX.characters( , 1)
270SAX.endElement(EXAMPLE)
271SAX.endDocument()</pre>
272
273<p>Most of the other functionnalities of libxml are based on the DOM tree
274building facility, so nearly everything up to the end of this document
275presuppose the use of the standard DOM tree build. Note that the DOM tree
276itself is built by a set of registered default callbacks, without internal
277specific interface.</p>
278
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000279<h2><a name="library">The XML library interfaces</a></h2>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000280
281<p>This section is directly intended to help programmers getting bootstrapped
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000282using the XML library from the C language. It doesn't intent to be extensive,
283I hope the automatically generated docs will provide the completeness
284required, but as a separated set of documents. The interfaces of the XML
285library are by principle low level, there is nearly zero abstration. Those
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000286interested in a higher level API should <a href="#DOM">look at DOM</a>.</p>
Daniel Veillardccb09631998-10-27 06:21:04 +0000287
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000288<h3><a name="Invoking">Invoking the parser</a></h3>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000289
290<p>Usually, the first thing to do is to read an XML input, the parser accepts
291to parse both memory mapped documents or direct files. The functions are
292defined in "parser.h":</p>
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000293<dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000294 <dt><code>xmlDocPtr xmlParseMemory(char *buffer, int size);</code></dt>
295 <dd><p>parse a zero terminated string containing the document</p>
296 </dd>
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000297</dl>
298<dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000299 <dt><code>xmlDocPtr xmlParseFile(const char *filename);</code></dt>
300 <dd><p>parse an XML document contained in a file (possibly compressed)</p>
301 </dd>
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000302</dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000303
304<p>This returns a pointer to the document structure (or NULL in case of
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000305failure).</p>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000306
307<p>A couple of comments can be made, first this mean that the parser is
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000308memory-hungry, first to load the document in memory, second to build the tree.
309Reading a document without building the tree will be possible in the future by
310pluggin the code to the SAX interface (see SAX.c).</p>
Daniel Veillardccb09631998-10-27 06:21:04 +0000311
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000312<h3><a name="Building">Building a tree from scratch</a></h3>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000313
314<p>The other way to get an XML tree in memory is by building it. Basically
315there is a set of functions dedicated to building new elements, those are also
Daniel Veillard25940b71998-10-29 05:51:30 +0000316described in "tree.h", here is for example the piece of code producing the
317example used before:</p>
318<pre> xmlDocPtr doc;
319 xmlNodePtr tree, subtree;
320
321 doc = xmlNewDoc("1.0");
322 doc->root = xmlNewDocNode(doc, NULL, "EXAMPLE", NULL);
323 xmlSetProp(doc->root, "prop1", "gnome is great");
324 xmlSetProp(doc->root, "prop2", "&amp;linux; too");
325 tree = xmlNewChild(doc->root, NULL, "head", NULL);
326 subtree = xmlNewChild(tree, NULL, "title", "Welcome to Gnome");
327 tree = xmlNewChild(doc->root, NULL, "chapter", NULL);
328 subtree = xmlNewChild(tree, NULL, "title", "The Linux adventure");
329 subtree = xmlNewChild(tree, NULL, "p", "bla bla bla ...");
330 subtree = xmlNewChild(tree, NULL, "image", NULL);
331 xmlSetProp(subtree, "href", "linus.gif");</pre>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000332
333<p>Not really rocket science ...</p>
Daniel Veillard25940b71998-10-29 05:51:30 +0000334
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000335<h3><a name="Traversing">Traversing the tree</a></h3>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000336
337<p>Basically by including "tree.h" your code has access to the internal
338structure of all the element of the tree. The names should be somewhat simple
339like <strong>parent</strong>, <strong>childs</strong>, <strong>next</strong>,
Daniel Veillard25940b71998-10-29 05:51:30 +0000340<strong>prev</strong>, <strong>properties</strong>, etc... For example still
341with the previous example:</p>
342<pre><code>doc->root->childs->childs</code></pre>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000343
344<p>points to the title element,</p>
Daniel Veillard25940b71998-10-29 05:51:30 +0000345<pre>doc->root->childs->next->child->child</pre>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000346
347<p>points to the text node containing the chapter titlle "The Linux adventure"
Daniel Veillard25940b71998-10-29 05:51:30 +0000348and</p>
349<pre>doc->root->properties->next->val</pre>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000350
351<p>points to the entity reference containing the value of "&amp;linux" at the
Daniel Veillard25940b71998-10-29 05:51:30 +0000352beginning of the second attribute of the root element "EXAMPLE".</p>
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000353
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000354<h3><a name="Modifying">Modifying the tree</a></h3>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000355
356<p>functions are provided to read and write the document content:</p>
Daniel Veillard25940b71998-10-29 05:51:30 +0000357<dl>
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000358 <dt><code>xmlAttrPtr xmlSetProp(xmlNodePtr node, const xmlChar *name, const
359 xmlChar *value);</code></dt>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000360 <dd><p>This set (or change) an attribute carried by an ELEMENT node the
361 value can be NULL</p>
362 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000363</dl>
364<dl>
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000365 <dt><code>const xmlChar *xmlGetProp(xmlNodePtr node, const xmlChar
Daniel Veillardb05deb71999-08-10 19:04:08 +0000366 *name);</code></dt>
367 <dd><p>This function returns a pointer to the property content, note that
368 no extra copy is made</p>
369 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000370</dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000371
372<p>Two functions must be used to read an write the text associated to
Daniel Veillard25940b71998-10-29 05:51:30 +0000373elements:</p>
374<dl>
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000375 <dt><code>xmlNodePtr xmlStringGetNodeList(xmlDocPtr doc, const xmlChar
Daniel Veillardb05deb71999-08-10 19:04:08 +0000376 *value);</code></dt>
377 <dd><p>This function takes an "external" string and convert it to one text
378 node or possibly to a list of entity and text nodes. All non-predefined
379 entity references like &amp;Gnome; will be stored internally as an
380 entity node, hence the result of the function may not be a single
381 node.</p>
382 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000383</dl>
384<dl>
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000385 <dt><code>xmlChar *xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int
Daniel Veillardb05deb71999-08-10 19:04:08 +0000386 inLine);</code></dt>
387 <dd><p>this is the dual function, which generate a new string containing
388 the content of the text and entity nodes. Note the extra argument
389 inLine, if set to 1 instead of returning the &amp;Gnome; XML encoding in
390 the string it will substitute it with it's value say "GNU Network Object
391 Model Environment". Set it if you want to use the string for non XML
392 usage like User Interface.</p>
393 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000394</dl>
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000395
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000396<h3><a name="Saving">Saving a tree</a></h3>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000397
398<p>Basically 3 options are possible:</p>
Daniel Veillard25940b71998-10-29 05:51:30 +0000399<dl>
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000400 <dt><code>void xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int
Daniel Veillardb05deb71999-08-10 19:04:08 +0000401 *size);</code></dt>
402 <dd><p>returns a buffer where the document has been saved</p>
403 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000404</dl>
405<dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000406 <dt><code>extern void xmlDocDump(FILE *f, xmlDocPtr doc);</code></dt>
407 <dd><p>dumps a buffer to an open file descriptor</p>
408 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000409</dl>
410<dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000411 <dt><code>int xmlSaveFile(const char *filename, xmlDocPtr cur);</code></dt>
412 <dd><p>save the document ot a file. In that case the compression interface
413 is triggered if turned on</p>
414 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000415</dl>
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000416
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000417<h3><a name="Compressio">Compression</a></h3>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000418
419<p>The library handle transparently compression when doing file based
420accesses, the level of compression on saves can be tuned either globally or
421individually for one file:</p>
Daniel Veillard25940b71998-10-29 05:51:30 +0000422<dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000423 <dt><code>int xmlGetDocCompressMode (xmlDocPtr doc);</code></dt>
424 <dd><p>Get the document compression ratio (0-9)</p>
425 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000426</dl>
427<dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000428 <dt><code>void xmlSetDocCompressMode (xmlDocPtr doc, int mode);</code></dt>
429 <dd><p>Set the document compression ratio</p>
430 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000431</dl>
432<dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000433 <dt><code>int xmlGetCompressMode(void);</code></dt>
434 <dd><p>Get the default compression ratio</p>
435 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000436</dl>
437<dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000438 <dt><code>void xmlSetCompressMode(int mode);</code></dt>
439 <dd><p>set the default compression ratio</p>
440 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000441</dl>
442
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000443<h2><a name="Entities">Entities or no entities</a></h2>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000444
445<p>Entities principle is similar to simple C macros. They define an
446abbreviation for a given string that you can reuse many time through the
447content of your document. They are especially useful when frequent occurrences
448of a given string may occur within a document or to confine the change needed
449to a document to a restricted area in the internal subset of the document (at
450the beginning). Example:</p>
451<pre>1 &lt;?xml version="1.0"?>
4522 &lt;!DOCTYPE EXAMPLE SYSTEM "example.dtd" [
4533 &lt;!ENTITY xml "Extensible Markup Language">
4544 ]>
4555 &lt;EXAMPLE>
4566 &amp;xml;
Daniel Veillard35008381999-10-25 13:15:52 +00004577 &lt;/EXAMPLE></pre>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000458
459<p>Line 3 declares the xml entity. Line 6 uses the xml entity, by prefixing
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000460it's name with '&amp;' and following it by ';' without any spaces added. There
461are 5 predefined entities in libxml allowing to escape charaters with
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000462predefined meaning in some parts of the xml document content:
463<strong>&amp;lt;</strong> for the letter '&lt;', <strong>&amp;gt;</strong> for
464the letter '>', <strong>&amp;apos;</strong> for the letter ''',
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000465<strong>&amp;quot;</strong> for the letter '"', and <strong>&amp;amp;</strong>
466for the letter '&amp;'.</p>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000467
468<p>One of the problems related to entities is that you may want the parser to
469substitute entities content to see the replacement text in your application,
470or you may prefer keeping entities references as such in the content to be
471able to save the document back without loosing this usually precious
472information (if the user went through the pain of explicitley defining
473entities, he may have a a rather negative attitude if you blindly susbtitute
474them as saving time). The function <a
475href="gnome-xml-parser.html#XMLSUBSTITUTEENTITIESDEFAULT">xmlSubstituteEntitiesDefault()</a>
476allows to check and change the behaviour, which is to not substitute entities
477by default.</p>
478
479<p>Here is the DOM tree built by libxml for the previous document in the
480default case:</p>
481<pre>/gnome/src/gnome-xml -> ./tester --debug test/ent1
482DOCUMENT
483version=1.0
484 ELEMENT EXAMPLE
485 TEXT
486 content=
487 ENTITY_REF
488 INTERNAL_GENERAL_ENTITY xml
489 content=Extensible Markup Language
490 TEXT
491 content=</pre>
492
493<p>And here is the result when substituting entities:</p>
494<pre>/gnome/src/gnome-xml -> ./tester --debug --noent test/ent1
495DOCUMENT
496version=1.0
497 ELEMENT EXAMPLE
498 TEXT
499 content= Extensible Markup Language</pre>
500
501<p>So entities or no entities ? Basically it depends on your use case, I
502suggest to keep the non-substituting default behaviour and avoid using
503entities in your XML document or data if you are not willing to handle the
504entity references elements in the DOM tree.</p>
505
506<p>Note that at save time libxml enforce the conversion of the predefined
507entities where necessary to prevent well-formedness problems, and will also
508transparently replace those with chars (i.e. will not generate entity
509reference elements in the DOM tree nor call the reference() SAX callback when
510finding them in the input).</p>
511
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000512<h2><a name="Namespaces">Namespaces</a></h2>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000513
514<p>The libxml library implement namespace @@ support by recognizing namespace
515contructs in the input, and does namespace lookup automatically when building
516the DOM tree. A namespace declaration is associated with an in-memory
517structure and all elements or attributes within that namespace point to it.
518Hence testing the namespace is a simple and fast equality operation at the
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000519user level.</p>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000520
521<p>I suggest it that people using libxml use a namespace, and declare it on
522the root element of their document as the default namespace. Then they dont
523need to happend the prefix in the content but we will have a basis for future
524semantic refinement and merging of data from different sources. This doesn't
525augment significantly the size of the XML output, but significantly increase
526it's value in the long-term.</p>
527
528<p>Concerning the namespace value, this has to be an URL, but this doesn't
529have to point to any existing resource on the Web. I suggest using an URL
530within a domain you control, which makes sense and if possible holding some
531kind of versionning informations. For example
532<code>"http://www.gnome.org/gnumeric/1.0"</code> is a good namespace scheme.
533Then when you load a file, make sure that a namespace carrying the
534version-independant prefix is installed on the root element of your document,
535and if the version information don't match something you know, warn the user
536and be liberal in what you accept as the input. Also do *not* try to base
537namespace checking on the prefix value &lt;foo:text> may be exactly the same
538as &lt;bar:text> in another document, what really matter is the URI
539associated with the element or the attribute, not the prefix string which is
540just a shortcut for the full URI.</p>
541
542<p>@@Interfaces@@</p>
543
544<p>@@Examples@@</p>
545
546<p>Usually people object using namespace in the case of validation, I object
547this and will make sure that using namespaces won't break validity checking,
548so even is you plan or are using validation I strongly suggest to add
549namespaces to your document. A default namespace scheme
550<code>xmlns="http://...."</code> should not break validity even on less
551flexible parsers. Now using namespace to mix and differenciate content coming
552from mutliple Dtd will certainly break current validation schemes, I will try
553to provide ways to do this, but this may not be portable or standardized.</p>
554
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000555<h2><a name="Validation">Validation, or are you afraid of DTDs ?</a></h2>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000556
557<p>Well what is validation and what is a DTD ?</p>
558
559<p>Validation is the process of checking a document against a set of
560construction rules, a <strong>DTD</strong> (Document Type Definition) is such
561a set of rules.</p>
562
563<p>The validation process and building DTDs are the two most difficult parts
564of XML life cycle. Briefly a DTD defines all the possibles element to be
565found within your document, what is the formal shape of your document tree (by
566defining the allowed content of an element, either text, a regular expression
567for the allowed list of children, or mixed content i.e. both text and childs).
568The DTD also defines the allowed attributes for all elements and the types of
569the attributes. For more detailed informations, I suggest to read the related
570parts of the XML specification, the examples found under
571gnome-xml/test/valid/dtd and the large amount of books available on XML. The
572dia example in gnome-xml/test/valid should be both simple and complete enough
573to allow you to build your own.</p>
574
575<p>A word of warning, building a good DTD which will fit your needs of your
576application in the long-term is far from trivial, however the extra level of
577quality it can insure is well worth the price for some sets of applications or
578if you already have already a DTD defined for your application field.</p>
579
580<p>The validation is not completely finished but in a (very IMHO) usable
581state. Until a real validation interface is defined the way to do it is to
582define and set the <strong>xmlDoValidityCheckingDefaultValue</strong> external
583variable to 1, this will of course be changed at some point:</p>
584
585<p>extern int xmlDoValidityCheckingDefaultValue;</p>
586
587<p>...</p>
588
589<p>xmlDoValidityCheckingDefaultValue = 1;</p>
590
591<p></p>
592
593<p>To handle external entities, use the function
594<strong>xmlSetExternalEntityLoader</strong>(xmlExternalEntityLoader f); to
595link in you HTTP/FTP/Entities database library to the standard libxml
596core.</p>
597
598<p>@@interfaces@@</p>
599
Daniel Veillard35008381999-10-25 13:15:52 +0000600<h2><a name="DOM"></a><a name="Principles">DOM Principles</a></h2>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000601
602<p><a href="http://www.w3.org/DOM/">DOM</a> stands for the <em>Document Object
Daniel Veillardccb09631998-10-27 06:21:04 +0000603Model</em> this is an API for accessing XML or HTML structured documents.
604Native support for DOM in Gnome is on the way (module gnome-dom), and it will
Daniel Veillard25940b71998-10-29 05:51:30 +0000605be based on gnome-xml. This will be a far cleaner interface to manipulate XML
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000606files within Gnome since it won't expose the internal structure. DOM defines a
Daniel Veillard25940b71998-10-29 05:51:30 +0000607set of IDL (or Java) interfaces allowing to traverse and manipulate a
608document. The DOM library will allow accessing and modifying "live" documents
609presents on other programs like this:</p>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000610
611<p><img src="DOM.gif" alt=" DOM.gif "></p>
612
613<p>This should help greatly doing things like modifying a gnumeric spreadsheet
Daniel Veillardccb09631998-10-27 06:21:04 +0000614embedded in a GWP document for example.</p>
Daniel Veillard14fff061999-06-22 21:49:07 +0000615
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000616<p>The current DOM implementation on top of libxml is the <a
617href="http://cvs.gnome.org/lxr/source/gdome/">gdome Gnome module</a>, this is
618a full DOM interface, thanks to <a href="mailto:raph@levien.com">Raph
619Levien</a>.</p>
620
621<p>The gnome-dom module in the Gnome CVS base is obsolete</p>
622
Daniel Veillard35008381999-10-25 13:15:52 +0000623<h2><a name="Example"></a><a name="real">A real example</a></h2>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000624
625<p>Here is a real size example, where the actual content of the application
626data is not kept in the DOM tree but uses internal structures. It is based on
Daniel Veillard14fff061999-06-22 21:49:07 +0000627a proposal to keep a database of jobs related to Gnome, with an XML based
Daniel Veillardb05deb71999-08-10 19:04:08 +0000628storage structure. Here is an <a href="gjobs.xml">XML encoded jobs
629base</a>:</p>
630<pre>&lt;?xml version="1.0"?>
Daniel Veillard14fff061999-06-22 21:49:07 +0000631&lt;gjob:Helping xmlns:gjob="http://www.gnome.org/some-location">
632 &lt;gjob:Jobs>
633
634 &lt;gjob:Job>
635 &lt;gjob:Project ID="3"/>
636 &lt;gjob:Application>GBackup&lt;/gjob:Application>
637 &lt;gjob:Category>Development&lt;/gjob:Category>
638
639 &lt;gjob:Update>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000640 &lt;gjob:Status>Open&lt;/gjob:Status>
641 &lt;gjob:Modified>Mon, 07 Jun 1999 20:27:45 -0400 MET DST&lt;/gjob:Modified>
Daniel Veillard14fff061999-06-22 21:49:07 +0000642 &lt;gjob:Salary>USD 0.00&lt;/gjob:Salary>
643 &lt;/gjob:Update>
644
645 &lt;gjob:Developers>
646 &lt;gjob:Developer>
647 &lt;/gjob:Developer>
648 &lt;/gjob:Developers>
649
650 &lt;gjob:Contact>
651 &lt;gjob:Person>Nathan Clemons&lt;/gjob:Person>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000652 &lt;gjob:Email>nathan@windsofstorm.net&lt;/gjob:Email>
Daniel Veillard14fff061999-06-22 21:49:07 +0000653 &lt;gjob:Company>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000654 &lt;/gjob:Company>
Daniel Veillard14fff061999-06-22 21:49:07 +0000655 &lt;gjob:Organisation>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000656 &lt;/gjob:Organisation>
Daniel Veillard14fff061999-06-22 21:49:07 +0000657 &lt;gjob:Webpage>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000658 &lt;/gjob:Webpage>
659 &lt;gjob:Snailmail>
660 &lt;/gjob:Snailmail>
661 &lt;gjob:Phone>
662 &lt;/gjob:Phone>
Daniel Veillard14fff061999-06-22 21:49:07 +0000663 &lt;/gjob:Contact>
664
665 &lt;gjob:Requirements>
666 The program should be released as free software, under the GPL.
667 &lt;/gjob:Requirements>
668
669 &lt;gjob:Skills>
670 &lt;/gjob:Skills>
671
672 &lt;gjob:Details>
673 A GNOME based system that will allow a superuser to configure
674 compressed and uncompressed files and/or file systems to be backed
675 up with a supported media in the system. This should be able to
676 perform via find commands generating a list of files that are passed
677 to tar, dd, cpio, cp, gzip, etc., to be directed to the tape machine
678 or via operations performed on the filesystem itself. Email
679 notification and GUI status display very important.
680 &lt;/gjob:Details>
681
682 &lt;/gjob:Job>
683
684 &lt;/gjob:Jobs>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000685&lt;/gjob:Helping></pre>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000686
687<p>While loading the XML file into an internal DOM tree is a matter of calling
688only a couple of functions, browsing the tree to gather the informations and
689generate the internals structures is harder, and more error prone.</p>
690
691<p>The suggested principle is to be tolerant with respect to the input
692structure. For example the ordering of the attributes is not significant, Cthe
693XML specification is clear about it. It's also usually a good idea to not be
694dependant of the orders of the childs of a given node, unless it really makes
695things harder. Here is some code to parse the informations for a person:</p>
696<pre>/*
Daniel Veillard14fff061999-06-22 21:49:07 +0000697 * A person record
698 */
699typedef struct person {
700 char *name;
701 char *email;
702 char *company;
703 char *organisation;
704 char *smail;
705 char *webPage;
706 char *phone;
707} person, *personPtr;
708
709/*
710 * And the code needed to parse it
711 */
712personPtr parsePerson(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) {
713 personPtr ret = NULL;
714
715DEBUG("parsePerson\n");
716 /*
717 * allocate the struct
718 */
719 ret = (personPtr) malloc(sizeof(person));
720 if (ret == NULL) {
721 fprintf(stderr,"out of memory\n");
Daniel Veillardb05deb71999-08-10 19:04:08 +0000722 return(NULL);
Daniel Veillard14fff061999-06-22 21:49:07 +0000723 }
724 memset(ret, 0, sizeof(person));
725
726 /* We don't care what the top level element name is */
727 cur = cur->childs;
728 while (cur != NULL) {
729 if ((!strcmp(cur->name, "Person")) &amp;&amp; (cur->ns == ns))
Daniel Veillardb05deb71999-08-10 19:04:08 +0000730 ret->name = xmlNodeListGetString(doc, cur->childs, 1);
Daniel Veillard14fff061999-06-22 21:49:07 +0000731 if ((!strcmp(cur->name, "Email")) &amp;&amp; (cur->ns == ns))
Daniel Veillardb05deb71999-08-10 19:04:08 +0000732 ret->email = xmlNodeListGetString(doc, cur->childs, 1);
733 cur = cur->next;
Daniel Veillard14fff061999-06-22 21:49:07 +0000734 }
735
736 return(ret);
Daniel Veillardb05deb71999-08-10 19:04:08 +0000737}</pre>
738
739<p>Here is a couple of things to notice:</p>
Daniel Veillard14fff061999-06-22 21:49:07 +0000740<ul>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000741 <li>Usually a recursive parsing style is the more convenient one, XML data
742 being by nature subject to repetitive constructs and usualy exibit highly
743 stuctured patterns.</li>
744 <li>The two arguments of type <em>xmlDocPtr</em> and <em>xmlNsPtr</em>, i.e.
745 the pointer to the global XML document and the namespace reserved to the
746 application. Document wide information are needed for example to decode
747 entities and it's a good coding practice to define a namespace for your
748 application set of data and test that the element and attributes you're
749 analyzing actually pertains to your application space. This is done by a
750 simple equality test (cur->ns == ns).</li>
751 <li>To retrieve text and attributes value, it is suggested to use the
752 function <em>xmlNodeListGetString</em> to gather all the text and entity
753 reference nodes generated by the DOM output and produce an single text
754 string.</li>
Daniel Veillard14fff061999-06-22 21:49:07 +0000755</ul>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000756
757<p>Here is another piece of code used to parse another level of the
758structure:</p>
759<pre>/*
Daniel Veillard14fff061999-06-22 21:49:07 +0000760 * a Description for a Job
761 */
762typedef struct job {
763 char *projectID;
764 char *application;
765 char *category;
766 personPtr contact;
767 int nbDevelopers;
768 personPtr developers[100]; /* using dynamic alloc is left as an exercise */
769} job, *jobPtr;
770
771/*
772 * And the code needed to parse it
773 */
774jobPtr parseJob(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) {
775 jobPtr ret = NULL;
776
777DEBUG("parseJob\n");
778 /*
779 * allocate the struct
780 */
781 ret = (jobPtr) malloc(sizeof(job));
782 if (ret == NULL) {
783 fprintf(stderr,"out of memory\n");
Daniel Veillardb05deb71999-08-10 19:04:08 +0000784 return(NULL);
Daniel Veillard14fff061999-06-22 21:49:07 +0000785 }
786 memset(ret, 0, sizeof(job));
787
788 /* We don't care what the top level element name is */
789 cur = cur->childs;
790 while (cur != NULL) {
791
792 if ((!strcmp(cur->name, "Project")) &amp;&amp; (cur->ns == ns)) {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000793 ret->projectID = xmlGetProp(cur, "ID");
794 if (ret->projectID == NULL) {
795 fprintf(stderr, "Project has no ID\n");
796 }
797 }
Daniel Veillard14fff061999-06-22 21:49:07 +0000798 if ((!strcmp(cur->name, "Application")) &amp;&amp; (cur->ns == ns))
Daniel Veillardb05deb71999-08-10 19:04:08 +0000799 ret->application = xmlNodeListGetString(doc, cur->childs, 1);
Daniel Veillard14fff061999-06-22 21:49:07 +0000800 if ((!strcmp(cur->name, "Category")) &amp;&amp; (cur->ns == ns))
Daniel Veillardb05deb71999-08-10 19:04:08 +0000801 ret->category = xmlNodeListGetString(doc, cur->childs, 1);
Daniel Veillard14fff061999-06-22 21:49:07 +0000802 if ((!strcmp(cur->name, "Contact")) &amp;&amp; (cur->ns == ns))
Daniel Veillardb05deb71999-08-10 19:04:08 +0000803 ret->contact = parsePerson(doc, ns, cur);
804 cur = cur->next;
Daniel Veillard14fff061999-06-22 21:49:07 +0000805 }
806
807 return(ret);
Daniel Veillardb05deb71999-08-10 19:04:08 +0000808}</pre>
Daniel Veillard14fff061999-06-22 21:49:07 +0000809
Daniel Veillardb05deb71999-08-10 19:04:08 +0000810<p>One can notice that once used to it, writing this kind of code is quite
811simple, but boring. Ultimately, it could be possble to write stubbers taking
812either C data structure definitions, a set of XML examples or an XML DTD and
813produce the code needed to import and export the content between C data and
814XML storage. This is left as an exercise to the reader :-)</p>
815
816<p>Feel free to use <a href="gjobread.c">the code for the full C parsing
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000817example</a> as a template, it is also available with Makefile in the Gnome CVS
818base under gnome-xml/example</p>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000819
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000820<p></p>
821
822<p><a href="mailto:Daniel.Veillard@w3.org">Daniel Veillard</a></p>
823
Daniel Veillard35008381999-10-25 13:15:52 +0000824<p>$Id: xml.html,v 1.10 1999/09/24 14:03:48 veillard Exp $</p>
Daniel Veillardccb09631998-10-27 06:21:04 +0000825</body>
826</html>