blob: 23a33f02b1e14575f85a066e31b8f41816aba1ce [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>
Daniel Veillardb24054a1999-12-18 15:32:46 +00006 <meta name="GENERATOR" content="amaya V2.4">
7 <meta http-equiv="Content-Type" content="text/html">
Daniel Veillardccb09631998-10-27 06:21:04 +00008</head>
Daniel Veillardccb09631998-10-27 06:21:04 +00009
Daniel Veillardb05deb71999-08-10 19:04:08 +000010<body bgcolor="#ffffff">
Daniel Veillardccb09631998-10-27 06:21:04 +000011<h1 align="center">The XML library for Gnome</h1>
Daniel Veillardb05deb71999-08-10 19:04:08 +000012
Daniel Veillardc8eab3a1999-09-04 18:27:23 +000013<h2 style="text-align: center">libxml, a.k.a. gnome-xml</h2>
14
15<p></p>
Daniel Veillard2f4dfc41999-09-24 14:03:48 +000016<ul>
17 <li><a href="#Introducti">Introduction</a></li>
18 <li><a href="#Documentat">Documentation</a></li>
Daniel Veillard10a2c651999-12-12 13:03:50 +000019 <li><a href="#Downloads">Downloads</a></li>
Daniel Veillard2f4dfc41999-09-24 14:03:48 +000020 <li><a href="#News">News</a></li>
21 <li><a href="#XML">XML</a></li>
22 <li><a href="#tree">The tree output</a></li>
23 <li><a href="#interface">The SAX interface</a></li>
24 <li><a href="#library">The XML library interfaces</a>
25 <ul>
26 <li><a href="#Invoking">Invoking the parser</a></li>
27 <li><a href="#Building">Building a tree from scratch</a></li>
28 <li><a href="#Traversing">Traversing the tree</a></li>
29 <li><a href="#Modifying">Modifying the tree</a></li>
30 <li><a href="#Saving">Saving the tree</a></li>
31 <li><a href="#Compressio">Compression</a></li>
32 </ul>
33 </li>
34 <li><a href="#Entities">Entities or no entities</a></li>
35 <li><a href="#Namespaces">Namespaces</a></li>
36 <li><a href="#Validation">Validation</a></li>
37 <li><a href="#Principles">DOM principles</a></li>
38 <li><a href="#real">A real example</a></li>
39</ul>
40
41<h2><a name="Introducti">Introduction</a></h2>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +000042
Daniel Veillardb05deb71999-08-10 19:04:08 +000043<p>This document describes the <a href="http://www.w3.org/XML/">XML</a>
44library provideed in the <a href="http://www.gnome.org/">Gnome</a> framework.
Daniel Veillardc8eab3a1999-09-04 18:27:23 +000045XML is a standard to build tag based structured documents/data.</p>
Daniel Veillardb05deb71999-08-10 19:04:08 +000046
47<p>The internal document repesentation is as close as possible to the <a
Daniel Veillardc8eab3a1999-09-04 18:27:23 +000048href="http://www.w3.org/DOM/">DOM</a> interfaces.</p>
Daniel Veillardb05deb71999-08-10 19:04:08 +000049
50<p>Libxml also has a <a href="http://www.megginson.com/SAX/index.html">SAX
51interface</a>, <a href="mailto:james@daa.com.au">James Henstridge</a> made <a
52href="http://www.daa.com.au/~james/gnome/xml-sax/xml-sax.html">a nice
53documentation</a> expaining how to use it. The interface is as compatible as
54possible with <a href="http://www.jclark.com/xml/expat.html">Expat</a>
55one.</p>
56
Daniel Veillardb05deb71999-08-10 19:04:08 +000057<p>There is also a mailing-list <a
Daniel Veillard6bd26dc1999-09-03 14:28:40 +000058href="mailto:xml@rufus.w3.org">xml@rufus.w3.org</a> for libxml, with an <a
Daniel Veillard944b5ff1999-12-15 19:08:24 +000059href="http://xmlsoft.org/messages">on-line archive</a>. To subscribe to this
60majordomo based list, send a mail to <a
Daniel Veillard6bd26dc1999-09-03 14:28:40 +000061href="mailto:majordomo@rufus.w3.org">majordomo@rufus.w3.org</a> with
62"subscribe xml" in the <strong>content</strong> of the message.</p>
Daniel Veillardb05deb71999-08-10 19:04:08 +000063
64<p>This library is released both under the W3C Copyright and the GNU LGP,
65basically everybody should be happy, if not, drop me a mail.</p>
66
67<p>People are invited to use the <a
68href="http://cvs.gnome.org/lxr/source/gdome/">gdome Gnome module to</a> get a
69full DOM interface, thanks to <a href="mailto:raph@levien.com">Raph
70Levien</a>, check his <a
71href="http://www.levien.com/gnome/domination.html">DOMination paper</a>. He
72uses it for his implementation of <a
73href="http://www.w3.org/Graphics/SVG/">SVG</a> called <a
74href="http://www.levien.com/svg/">gill</a>.</p>
Daniel Veillardccb09631998-10-27 06:21:04 +000075
Daniel Veillard2f4dfc41999-09-24 14:03:48 +000076<h2><a name="Documentat">Documentation</a></h2>
Daniel Veillardb05deb71999-08-10 19:04:08 +000077
Daniel Veillardb24054a1999-12-18 15:32:46 +000078<p>The code is commented in a <a href=""></a>way which allow <a
Daniel Veillard944b5ff1999-12-15 19:08:24 +000079href="http://xmlsoft.org/libxml.html">extensive documentation</a> to be
80automatically extracted.</p>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +000081
82<p>At some point I will change the back-end to produce XML documentation in
83addition to SGML Docbook and HTML.</p>
84
Daniel Veillard4c3a2031999-11-19 17:46:26 +000085<h3>Reporting bugs</h3>
86
87<p>Well bugs or missing features are always possible, and I will make a point
88of fixing them in a timely fashion. The best way it to <a
89href="http://bugs.gnome.org/db/pa/lgnome-xml.html">use the Gnome bug tracking
90database</a>. I look at reports there regulary and it's good to have a
91reminder when a bug is still open. Check the <a
92href="http://bugs.gnome.org/Reporting.html">instructions on reporting bugs</a>
93and be sure to specify thatthe bug is for the package gnome-xml.</p>
94
95<p>Alternately you can just send the bug to the <a
Daniel Veillard10a2c651999-12-12 13:03:50 +000096href="mailto:xml@rufus.w3.org">xml@rufus.w3.org</a> list.</p>
Daniel Veillard4c3a2031999-11-19 17:46:26 +000097
Daniel Veillard10a2c651999-12-12 13:03:50 +000098<h2><a name="Downloads">Downloads</a></h2>
Daniel Veillard2f4dfc41999-09-24 14:03:48 +000099
Daniel Veillarde4e51311999-12-18 15:32:46 +0000100<p>Latest version is 1.8.1, you can find it on <a
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000101href="ftp://rpmfind.net/pub/veillard/">rpmfind.net</a> or on the <a
102href="ftp://ftp.gnome.org/pub/GNOME/MIRRORS.html">Gnome FTP server</a> either
Daniel Veillard10a2c651999-12-12 13:03:50 +0000103as a <a href="ftp://ftp.gnome.org/pub/GNOME/stable/sources/libxml/">source
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000104archive</a> or <a href="ftp://ftp.gnome.org/pub/GNOME/contrib/rpms/">RPMs
105packages</a>.</p>
106
Daniel Veillard10a2c651999-12-12 13:03:50 +0000107<p>Libxml is also available from 2 CVs bases:</p>
108<ul>
109 <li><p>The <a href="http://dev.w3.org/cvsweb/XML/">W3C CVS base</a>,
110 available read-only using the CVS pserver authentification:</p>
111 <pre>CVSROOT=:pserver:anonymous@dev.w3.org:/sources/public
112password: anonymous
113module: XML</pre>
114 </li>
115 <li><p>The <a
116 href="http://cvs.gnome.org/bonsai/rview.cgi?cvsroot=/cvs/gnome&amp;dir=gnome-xml">Gnome
117 CVS base</a>, Check the <a
118 href="http://developer.gnome.org/tools/cvs.html">Gnome CVS Tools</a> page,
119 the CVS module is <b>gnome-xml</b></p>
120 </li>
121</ul>
122
123<h2><a name="News">News</a></h2>
124
Daniel Veillard944b5ff1999-12-15 19:08:24 +0000125<h3>CVS only : check the <a
126href="http://cvs.gnome.org/lxr/source/gnome-xml/ChangeLog">Changelog</a> file
127for really accurate description</h3>
Daniel Veillard4c3a2031999-11-19 17:46:26 +0000128<ul>
Daniel Veillard10a2c651999-12-12 13:03:50 +0000129 <li>working on HTML and XML links recognition layers, get in touch with me
Daniel Veillard944b5ff1999-12-15 19:08:24 +0000130 if you want to test those.</li>
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000131 <li>a Push interface for the XML parser</li>
Daniel Veillard437b87b2000-01-03 17:30:46 +0000132 <li>an shell like interface to the document tree (try tester --shell
133 :-)</li>
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000134 <li>lots of bug fixes and improvement added over XMas hollidays</li>
Daniel Veillard437b87b2000-01-03 17:30:46 +0000135 <li>fixed the DTD parsing code to work with the xhtml DTD</li>
136 <li>added xmlRemoveProp()</li>
137 <li>Fixed bugs in xmlNewNs </li>
138 <li>External entity loading code has been revamped, now it uses
139 xmlLoadExternalEntity()</li>
140 <li>cleaned up WIN32 includes of socket stuff</li>
Daniel Veillard5cb5ab81999-12-21 15:35:29 +0000141</ul>
142
143<h3>1.8.2: Dec 21 1999</h3>
144<ul>
Daniel Veillardb24054a1999-12-18 15:32:46 +0000145 <li>I got another problem with includes and C++, I hope this issue is fixed
146 for good this time</li>
Daniel Veillard5cb5ab81999-12-21 15:35:29 +0000147 <li>Added a few tree modification functions: xmlReplaceNode,
148 xmlAddPrevSibling, xmlAddNextSibling, xmlNodeSetName and
149 xmlDocSetRootElement</li>
150 <li>Tried to improve the HTML output with help from <a
151 href="mailto:clahey@umich.edu">Chris Lahey</a></li>
Daniel Veillarde4e51311999-12-18 15:32:46 +0000152</ul>
Daniel Veillardb24054a1999-12-18 15:32:46 +0000153
Daniel Veillarde4e51311999-12-18 15:32:46 +0000154<h3>1.8.1: Dec 18 1999</h3>
155<ul>
156 <li>various patches to avoid troubles when using libxml with C++ compilers
157 the "namespace" keyword and C escaping in include files</li>
158 <li>a problem in one of the core macros IS_CHAR was corrected</li>
159 <li>fixed a bug introduced in 1.8.0 breaking default namespace processing,
160 and more specifically the Dia application</li>
Daniel Veillard944b5ff1999-12-15 19:08:24 +0000161 <li>fixed a posteriori validation (validation after parsing, or by using a
162 Dtd not specified in the original document)</li>
Daniel Veillardb24054a1999-12-18 15:32:46 +0000163 <li>fixed a bug in</li>
Daniel Veillard10a2c651999-12-12 13:03:50 +0000164</ul>
165
166<h3>1.8.0: Dec 12 1999</h3>
167<ul>
168 <li>cleanup, especially memory wise</li>
169 <li>the parser should be more reliable, especially the HTML one, it should
170 not crash, whatever the input !</li>
171 <li>Integrated various patches, especially a speedup improvement for large
172 dataset from <a href="mailto:cnygard@bellatlantic.net">Carl Nygard</a>,
173 configure with --with-buffers to enable them.</li>
174 <li>attribute normalization, oops should have been added long ago !</li>
175 <li>attributes defaulted from Dtds should be available, xmlSetProp() now
176 does entities escapting by default.</li>
Daniel Veillard4c3a2031999-11-19 17:46:26 +0000177</ul>
Daniel Veillard35008381999-10-25 13:15:52 +0000178
179<h3>1.7.4: Oct 25 1999</h3>
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000180<ul>
Daniel Veillard35008381999-10-25 13:15:52 +0000181 <li>Lots of HTML improvement</li>
182 <li>Fixed some errors when saving both XML and HTML</li>
183 <li>More examples, the regression tests should now look clean</li>
184 <li>Fixed a bug with contiguous charref</li>
185</ul>
186
187<h3>1.7.3: Sep 29 1999</h3>
188<ul>
189 <li>portability problems fixed</li>
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000190 <li>snprintf was used unconditionnally, leading to link problems on system
Daniel Veillard35008381999-10-25 13:15:52 +0000191 were it's not available, fixed</li>
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000192</ul>
193
194<h3>1.7.1: Sep 24 1999</h3>
195<ul>
196 <li>The basic type for strings manipulated by libxml has been renamed in
197 1.7.1 from <strong>CHAR</strong> to <strong>xmlChar</strong>. The reason
198 is that CHAR was conflicting with a predefined type on Windows. However on
199 non WIN32 environment, compatibility is provided by the way of a
200 <strong>#define </strong>.</li>
201 <li>Changed another error : the use of a structure field called errno, and
202 leading to troubles on platforms where it's a macro</li>
203</ul>
204
205<h3>1.7.0: sep 23 1999</h3>
206<ul>
207 <li>Added the ability to fetch remote DTD or parsed entities, see the <a
208 href="gnome-xml-nanohttp.html">nanohttp</a> module.</li>
209 <li>Added an errno to report errors by another mean than a simple printf
210 like callback</li>
211 <li>Finished ID/IDREF support and checking when validation</li>
212 <li>Serious memory leaks fixed (there is now a <a
213 href="gnome-xml-xmlmemory.html">memory wrapper</a> module)</li>
214 <li>Improvement of <a href="http://www.w3.org/TR/xpath">XPath</a>
215 implementation</li>
216 <li>Added an HTML parser front-end</li>
217</ul>
218
219<h2><a name="XML">XML</a></h2>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000220
221<p><a href="http://www.w3.org/TR/REC-xml">XML is a standard</a> for markup
222based structured documents, here is <a name="example">an example</a>:</p>
Daniel Veillardccb09631998-10-27 06:21:04 +0000223<pre>&lt;?xml version="1.0"?>
Daniel Veillard14fff061999-06-22 21:49:07 +0000224&lt;EXAMPLE prop1="gnome is great" prop2="&amp;amp; linux too">
Daniel Veillardccb09631998-10-27 06:21:04 +0000225 &lt;head>
226 &lt;title>Welcome to Gnome&lt;/title>
227 &lt;/head>
228 &lt;chapter>
229 &lt;title>The Linux adventure&lt;/title>
230 &lt;p>bla bla bla ...&lt;/p>
231 &lt;image href="linus.gif"/>
232 &lt;p>...&lt;/p>
233 &lt;/chapter>
234&lt;/EXAMPLE></pre>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000235
236<p>The first line specify that it's an XML document and gives useful
237informations about it's encoding. Then the document is a text format whose
238structure is specified by tags between brackets. <strong>Each tag opened have
239to be closed</strong> XML is pedantic about this, not that for example the
240image tag has no content (just an attribute) and is closed by ending up the
241tag with <code>/></code>.</p>
Daniel Veillardccb09631998-10-27 06:21:04 +0000242
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000243<p>XML can be applied sucessfully to a wide range or usage from long term
244structured document maintenance where it follows the steps of SGML to simple
245data encoding mechanism like configuration file format (glade), spreadsheets
246(gnumeric), or even shorter lived document like in WebDAV where it is used to
247encode remote call between a client and a server.</p>
248
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000249<h2><a name="tree">The tree output</a></h2>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000250
251<p>The parser returns a tree built during the document analysis. The value
Daniel Veillardccb09631998-10-27 06:21:04 +0000252returned is an <strong>xmlDocPtr</strong> (i.e. a pointer to an
253<strong>xmlDoc</strong> structure). This structure contains informations like
254the file name, the document type, and a <strong>root</strong> pointer which
255is the root of the document (or more exactly the first child under the root
256which is the document). The tree is made of <strong>xmlNode</strong>s, chained
257in double linked lists of siblings and with childs&lt;->parent relationship.
258An xmlNode can also carry properties (a chain of xmlAttr structures). An
259attribute may have a value which is a list of TEXT or ENTITY_REF nodes.</p>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000260
261<p>Here is an example (erroneous w.r.t. the XML spec since there should be
262only one ELEMENT under the root):</p>
263
264<p><img src="structure.gif" alt=" structure.gif "></p>
265
266<p>In the source package there is a small program (not installed by default)
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000267called <strong>tester</strong> which parses XML files given as argument and
268prints them back as parsed, this is useful to detect errors both in XML code
269and in the XML parser itself. It has an option <strong>--debug</strong> which
270prints the actual in-memory structure of the document, here is the result with
271the <a href="#example">example</a> given before:</p>
272<pre>DOCUMENT
273version=1.0
274standalone=true
275 ELEMENT EXAMPLE
276 ATTRIBUTE prop1
277 TEXT
278 content=gnome is great
279 ATTRIBUTE prop2
280 ENTITY_REF
281 TEXT
282 content= too
283 ELEMENT head
284 ELEMENT title
Daniel Veillard25940b71998-10-29 05:51:30 +0000285 TEXT
286 content=Welcome to Gnome
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000287 ELEMENT chapter
288 ELEMENT title
Daniel Veillard25940b71998-10-29 05:51:30 +0000289 TEXT
290 content=The Linux adventure
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000291 ELEMENT p
Daniel Veillard25940b71998-10-29 05:51:30 +0000292 TEXT
293 content=bla bla bla ...
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000294 ELEMENT image
295 ATTRIBUTE href
296 TEXT
297 content=linus.gif
298 ELEMENT p
Daniel Veillard25940b71998-10-29 05:51:30 +0000299 TEXT
300 content=...</pre>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000301
302<p>This should be useful to learn the internal representation model.</p>
Daniel Veillardccb09631998-10-27 06:21:04 +0000303
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000304<h2><a name="interface">The SAX interface</a></h2>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000305
306<p>Sometimes the DOM tree output is just to large to fit reasonably into
307memory. In that case and if you don't expect to save back the XML document
308loaded using libxml, it's better to use the SAX interface of libxml. SAX is a
309<strong>callback based interface</strong> to the parser. Before parsing, the
310application layer register a customized set of callbacks which will be called
311by the library as it progresses through the XML input.</p>
312
313<p>To get a more detailed step-by-step guidance on using the SAX interface of
314libxml, <a href="mailto:james@daa.com.au">James Henstridge</a> made <a
315href="http://www.daa.com.au/~james/gnome/xml-sax/xml-sax.html">a nice
316documentation.</a></p>
317
318<p>You can debug the SAX behaviour by using the <strong>testSAX</strong>
319program located in the gnome-xml module (it's usually not shipped in the
320binary packages of libxml, but you can also find it in the tar source
321distribution). Here is the sequence of callback that would be generated when
322parsing the example given before as reported by testSAX:</p>
323<pre>SAX.setDocumentLocator()
324SAX.startDocument()
325SAX.getEntity(amp)
326SAX.startElement(EXAMPLE, prop1='gnome is great', prop2='&amp;amp; linux too')
327SAX.characters( , 3)
328SAX.startElement(head)
329SAX.characters( , 4)
330SAX.startElement(title)
331SAX.characters(Welcome to Gnome, 16)
332SAX.endElement(title)
333SAX.characters( , 3)
334SAX.endElement(head)
335SAX.characters( , 3)
336SAX.startElement(chapter)
337SAX.characters( , 4)
338SAX.startElement(title)
339SAX.characters(The Linux adventure, 19)
340SAX.endElement(title)
341SAX.characters( , 4)
342SAX.startElement(p)
343SAX.characters(bla bla bla ..., 15)
344SAX.endElement(p)
345SAX.characters( , 4)
346SAX.startElement(image, href='linus.gif')
347SAX.endElement(image)
348SAX.characters( , 4)
349SAX.startElement(p)
350SAX.characters(..., 3)
351SAX.endElement(p)
352SAX.characters( , 3)
353SAX.endElement(chapter)
354SAX.characters( , 1)
355SAX.endElement(EXAMPLE)
356SAX.endDocument()</pre>
357
358<p>Most of the other functionnalities of libxml are based on the DOM tree
359building facility, so nearly everything up to the end of this document
360presuppose the use of the standard DOM tree build. Note that the DOM tree
361itself is built by a set of registered default callbacks, without internal
362specific interface.</p>
363
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000364<h2><a name="library">The XML library interfaces</a></h2>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000365
366<p>This section is directly intended to help programmers getting bootstrapped
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000367using the XML library from the C language. It doesn't intent to be extensive,
368I hope the automatically generated docs will provide the completeness
369required, but as a separated set of documents. The interfaces of the XML
370library are by principle low level, there is nearly zero abstration. Those
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000371interested in a higher level API should <a href="#DOM">look at DOM</a>.</p>
Daniel Veillardccb09631998-10-27 06:21:04 +0000372
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000373<h3><a name="Invoking">Invoking the parser</a></h3>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000374
375<p>Usually, the first thing to do is to read an XML input, the parser accepts
376to parse both memory mapped documents or direct files. The functions are
377defined in "parser.h":</p>
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000378<dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000379 <dt><code>xmlDocPtr xmlParseMemory(char *buffer, int size);</code></dt>
380 <dd><p>parse a zero terminated string containing the document</p>
381 </dd>
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000382</dl>
383<dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000384 <dt><code>xmlDocPtr xmlParseFile(const char *filename);</code></dt>
385 <dd><p>parse an XML document contained in a file (possibly compressed)</p>
386 </dd>
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000387</dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000388
389<p>This returns a pointer to the document structure (or NULL in case of
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000390failure).</p>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000391
392<p>A couple of comments can be made, first this mean that the parser is
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000393memory-hungry, first to load the document in memory, second to build the tree.
394Reading a document without building the tree will be possible in the future by
395pluggin the code to the SAX interface (see SAX.c).</p>
Daniel Veillardccb09631998-10-27 06:21:04 +0000396
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000397<h3><a name="Building">Building a tree from scratch</a></h3>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000398
399<p>The other way to get an XML tree in memory is by building it. Basically
400there is a set of functions dedicated to building new elements, those are also
Daniel Veillard25940b71998-10-29 05:51:30 +0000401described in "tree.h", here is for example the piece of code producing the
402example used before:</p>
403<pre> xmlDocPtr doc;
404 xmlNodePtr tree, subtree;
405
406 doc = xmlNewDoc("1.0");
407 doc->root = xmlNewDocNode(doc, NULL, "EXAMPLE", NULL);
408 xmlSetProp(doc->root, "prop1", "gnome is great");
409 xmlSetProp(doc->root, "prop2", "&amp;linux; too");
410 tree = xmlNewChild(doc->root, NULL, "head", NULL);
411 subtree = xmlNewChild(tree, NULL, "title", "Welcome to Gnome");
412 tree = xmlNewChild(doc->root, NULL, "chapter", NULL);
413 subtree = xmlNewChild(tree, NULL, "title", "The Linux adventure");
414 subtree = xmlNewChild(tree, NULL, "p", "bla bla bla ...");
415 subtree = xmlNewChild(tree, NULL, "image", NULL);
416 xmlSetProp(subtree, "href", "linus.gif");</pre>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000417
418<p>Not really rocket science ...</p>
Daniel Veillard25940b71998-10-29 05:51:30 +0000419
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000420<h3><a name="Traversing">Traversing the tree</a></h3>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000421
422<p>Basically by including "tree.h" your code has access to the internal
423structure of all the element of the tree. The names should be somewhat simple
424like <strong>parent</strong>, <strong>childs</strong>, <strong>next</strong>,
Daniel Veillard25940b71998-10-29 05:51:30 +0000425<strong>prev</strong>, <strong>properties</strong>, etc... For example still
426with the previous example:</p>
427<pre><code>doc->root->childs->childs</code></pre>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000428
429<p>points to the title element,</p>
Daniel Veillard25940b71998-10-29 05:51:30 +0000430<pre>doc->root->childs->next->child->child</pre>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000431
432<p>points to the text node containing the chapter titlle "The Linux adventure"
Daniel Veillard25940b71998-10-29 05:51:30 +0000433and</p>
434<pre>doc->root->properties->next->val</pre>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000435
436<p>points to the entity reference containing the value of "&amp;linux" at the
Daniel Veillard25940b71998-10-29 05:51:30 +0000437beginning of the second attribute of the root element "EXAMPLE".</p>
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000438
Daniel Veillardb24054a1999-12-18 15:32:46 +0000439<p><strong>NOTE</strong>: XML allows <em>PI</em>s and <em>comments</em> to be
440present before the document root, so doc->root may point to an element which
441is not the document Root Element, a function
Daniel Veillard5cb5ab81999-12-21 15:35:29 +0000442<code>xmlDocGetRootElement()</code> was added for this purpose.</p>
Daniel Veillardb24054a1999-12-18 15:32:46 +0000443
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000444<h3><a name="Modifying">Modifying the tree</a></h3>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000445
446<p>functions are provided to read and write the document content:</p>
Daniel Veillard25940b71998-10-29 05:51:30 +0000447<dl>
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000448 <dt><code>xmlAttrPtr xmlSetProp(xmlNodePtr node, const xmlChar *name, const
449 xmlChar *value);</code></dt>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000450 <dd><p>This set (or change) an attribute carried by an ELEMENT node the
451 value can be NULL</p>
452 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000453</dl>
454<dl>
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000455 <dt><code>const xmlChar *xmlGetProp(xmlNodePtr node, const xmlChar
Daniel Veillardb05deb71999-08-10 19:04:08 +0000456 *name);</code></dt>
457 <dd><p>This function returns a pointer to the property content, note that
458 no extra copy is made</p>
459 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000460</dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000461
462<p>Two functions must be used to read an write the text associated to
Daniel Veillard25940b71998-10-29 05:51:30 +0000463elements:</p>
464<dl>
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000465 <dt><code>xmlNodePtr xmlStringGetNodeList(xmlDocPtr doc, const xmlChar
Daniel Veillardb05deb71999-08-10 19:04:08 +0000466 *value);</code></dt>
467 <dd><p>This function takes an "external" string and convert it to one text
468 node or possibly to a list of entity and text nodes. All non-predefined
469 entity references like &amp;Gnome; will be stored internally as an
470 entity node, hence the result of the function may not be a single
471 node.</p>
472 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000473</dl>
474<dl>
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000475 <dt><code>xmlChar *xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int
Daniel Veillardb05deb71999-08-10 19:04:08 +0000476 inLine);</code></dt>
477 <dd><p>this is the dual function, which generate a new string containing
478 the content of the text and entity nodes. Note the extra argument
479 inLine, if set to 1 instead of returning the &amp;Gnome; XML encoding in
480 the string it will substitute it with it's value say "GNU Network Object
481 Model Environment". Set it if you want to use the string for non XML
482 usage like User Interface.</p>
483 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000484</dl>
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000485
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000486<h3><a name="Saving">Saving a tree</a></h3>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000487
488<p>Basically 3 options are possible:</p>
Daniel Veillard25940b71998-10-29 05:51:30 +0000489<dl>
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000490 <dt><code>void xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int
Daniel Veillardb05deb71999-08-10 19:04:08 +0000491 *size);</code></dt>
492 <dd><p>returns a buffer where the document has been saved</p>
493 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000494</dl>
495<dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000496 <dt><code>extern void xmlDocDump(FILE *f, xmlDocPtr doc);</code></dt>
497 <dd><p>dumps a buffer to an open file descriptor</p>
498 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000499</dl>
500<dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000501 <dt><code>int xmlSaveFile(const char *filename, xmlDocPtr cur);</code></dt>
502 <dd><p>save the document ot a file. In that case the compression interface
503 is triggered if turned on</p>
504 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000505</dl>
Daniel Veillard10c6a8f1998-10-28 01:00:12 +0000506
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000507<h3><a name="Compressio">Compression</a></h3>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000508
509<p>The library handle transparently compression when doing file based
510accesses, the level of compression on saves can be tuned either globally or
511individually for one file:</p>
Daniel Veillard25940b71998-10-29 05:51:30 +0000512<dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000513 <dt><code>int xmlGetDocCompressMode (xmlDocPtr doc);</code></dt>
514 <dd><p>Get the document compression ratio (0-9)</p>
515 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000516</dl>
517<dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000518 <dt><code>void xmlSetDocCompressMode (xmlDocPtr doc, int mode);</code></dt>
519 <dd><p>Set the document compression ratio</p>
520 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000521</dl>
522<dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000523 <dt><code>int xmlGetCompressMode(void);</code></dt>
524 <dd><p>Get the default compression ratio</p>
525 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000526</dl>
527<dl>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000528 <dt><code>void xmlSetCompressMode(int mode);</code></dt>
529 <dd><p>set the default compression ratio</p>
530 </dd>
Daniel Veillard25940b71998-10-29 05:51:30 +0000531</dl>
532
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000533<h2><a name="Entities">Entities or no entities</a></h2>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000534
535<p>Entities principle is similar to simple C macros. They define an
536abbreviation for a given string that you can reuse many time through the
537content of your document. They are especially useful when frequent occurrences
538of a given string may occur within a document or to confine the change needed
539to a document to a restricted area in the internal subset of the document (at
540the beginning). Example:</p>
541<pre>1 &lt;?xml version="1.0"?>
5422 &lt;!DOCTYPE EXAMPLE SYSTEM "example.dtd" [
5433 &lt;!ENTITY xml "Extensible Markup Language">
5444 ]>
5455 &lt;EXAMPLE>
5466 &amp;xml;
Daniel Veillard35008381999-10-25 13:15:52 +00005477 &lt;/EXAMPLE></pre>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000548
549<p>Line 3 declares the xml entity. Line 6 uses the xml entity, by prefixing
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000550it's name with '&amp;' and following it by ';' without any spaces added. There
551are 5 predefined entities in libxml allowing to escape charaters with
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000552predefined meaning in some parts of the xml document content:
553<strong>&amp;lt;</strong> for the letter '&lt;', <strong>&amp;gt;</strong> for
554the letter '>', <strong>&amp;apos;</strong> for the letter ''',
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000555<strong>&amp;quot;</strong> for the letter '"', and <strong>&amp;amp;</strong>
556for the letter '&amp;'.</p>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000557
558<p>One of the problems related to entities is that you may want the parser to
559substitute entities content to see the replacement text in your application,
560or you may prefer keeping entities references as such in the content to be
561able to save the document back without loosing this usually precious
562information (if the user went through the pain of explicitley defining
563entities, he may have a a rather negative attitude if you blindly susbtitute
564them as saving time). The function <a
565href="gnome-xml-parser.html#XMLSUBSTITUTEENTITIESDEFAULT">xmlSubstituteEntitiesDefault()</a>
566allows to check and change the behaviour, which is to not substitute entities
567by default.</p>
568
569<p>Here is the DOM tree built by libxml for the previous document in the
570default case:</p>
571<pre>/gnome/src/gnome-xml -> ./tester --debug test/ent1
572DOCUMENT
573version=1.0
574 ELEMENT EXAMPLE
575 TEXT
576 content=
577 ENTITY_REF
578 INTERNAL_GENERAL_ENTITY xml
579 content=Extensible Markup Language
580 TEXT
581 content=</pre>
582
583<p>And here is the result when substituting entities:</p>
584<pre>/gnome/src/gnome-xml -> ./tester --debug --noent test/ent1
585DOCUMENT
586version=1.0
587 ELEMENT EXAMPLE
588 TEXT
589 content= Extensible Markup Language</pre>
590
591<p>So entities or no entities ? Basically it depends on your use case, I
592suggest to keep the non-substituting default behaviour and avoid using
593entities in your XML document or data if you are not willing to handle the
594entity references elements in the DOM tree.</p>
595
596<p>Note that at save time libxml enforce the conversion of the predefined
597entities where necessary to prevent well-formedness problems, and will also
598transparently replace those with chars (i.e. will not generate entity
599reference elements in the DOM tree nor call the reference() SAX callback when
600finding them in the input).</p>
601
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000602<h2><a name="Namespaces">Namespaces</a></h2>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000603
604<p>The libxml library implement namespace @@ support by recognizing namespace
605contructs in the input, and does namespace lookup automatically when building
606the DOM tree. A namespace declaration is associated with an in-memory
607structure and all elements or attributes within that namespace point to it.
608Hence testing the namespace is a simple and fast equality operation at the
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000609user level.</p>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000610
611<p>I suggest it that people using libxml use a namespace, and declare it on
612the root element of their document as the default namespace. Then they dont
613need to happend the prefix in the content but we will have a basis for future
614semantic refinement and merging of data from different sources. This doesn't
615augment significantly the size of the XML output, but significantly increase
616it's value in the long-term.</p>
617
618<p>Concerning the namespace value, this has to be an URL, but this doesn't
619have to point to any existing resource on the Web. I suggest using an URL
620within a domain you control, which makes sense and if possible holding some
621kind of versionning informations. For example
622<code>"http://www.gnome.org/gnumeric/1.0"</code> is a good namespace scheme.
623Then when you load a file, make sure that a namespace carrying the
624version-independant prefix is installed on the root element of your document,
625and if the version information don't match something you know, warn the user
626and be liberal in what you accept as the input. Also do *not* try to base
627namespace checking on the prefix value &lt;foo:text> may be exactly the same
628as &lt;bar:text> in another document, what really matter is the URI
629associated with the element or the attribute, not the prefix string which is
630just a shortcut for the full URI.</p>
631
632<p>@@Interfaces@@</p>
633
634<p>@@Examples@@</p>
635
636<p>Usually people object using namespace in the case of validation, I object
637this and will make sure that using namespaces won't break validity checking,
638so even is you plan or are using validation I strongly suggest to add
639namespaces to your document. A default namespace scheme
640<code>xmlns="http://...."</code> should not break validity even on less
641flexible parsers. Now using namespace to mix and differenciate content coming
642from mutliple Dtd will certainly break current validation schemes, I will try
643to provide ways to do this, but this may not be portable or standardized.</p>
644
Daniel Veillard2f4dfc41999-09-24 14:03:48 +0000645<h2><a name="Validation">Validation, or are you afraid of DTDs ?</a></h2>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000646
647<p>Well what is validation and what is a DTD ?</p>
648
649<p>Validation is the process of checking a document against a set of
650construction rules, a <strong>DTD</strong> (Document Type Definition) is such
651a set of rules.</p>
652
653<p>The validation process and building DTDs are the two most difficult parts
654of XML life cycle. Briefly a DTD defines all the possibles element to be
655found within your document, what is the formal shape of your document tree (by
656defining the allowed content of an element, either text, a regular expression
657for the allowed list of children, or mixed content i.e. both text and childs).
658The DTD also defines the allowed attributes for all elements and the types of
659the attributes. For more detailed informations, I suggest to read the related
660parts of the XML specification, the examples found under
661gnome-xml/test/valid/dtd and the large amount of books available on XML. The
662dia example in gnome-xml/test/valid should be both simple and complete enough
663to allow you to build your own.</p>
664
665<p>A word of warning, building a good DTD which will fit your needs of your
666application in the long-term is far from trivial, however the extra level of
667quality it can insure is well worth the price for some sets of applications or
668if you already have already a DTD defined for your application field.</p>
669
670<p>The validation is not completely finished but in a (very IMHO) usable
671state. Until a real validation interface is defined the way to do it is to
672define and set the <strong>xmlDoValidityCheckingDefaultValue</strong> external
673variable to 1, this will of course be changed at some point:</p>
674
675<p>extern int xmlDoValidityCheckingDefaultValue;</p>
676
677<p>...</p>
678
679<p>xmlDoValidityCheckingDefaultValue = 1;</p>
680
681<p></p>
682
683<p>To handle external entities, use the function
684<strong>xmlSetExternalEntityLoader</strong>(xmlExternalEntityLoader f); to
685link in you HTTP/FTP/Entities database library to the standard libxml
686core.</p>
687
688<p>@@interfaces@@</p>
689
Daniel Veillard35008381999-10-25 13:15:52 +0000690<h2><a name="DOM"></a><a name="Principles">DOM Principles</a></h2>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000691
692<p><a href="http://www.w3.org/DOM/">DOM</a> stands for the <em>Document Object
Daniel Veillardccb09631998-10-27 06:21:04 +0000693Model</em> this is an API for accessing XML or HTML structured documents.
694Native support for DOM in Gnome is on the way (module gnome-dom), and it will
Daniel Veillard25940b71998-10-29 05:51:30 +0000695be based on gnome-xml. This will be a far cleaner interface to manipulate XML
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000696files within Gnome since it won't expose the internal structure. DOM defines a
Daniel Veillard25940b71998-10-29 05:51:30 +0000697set of IDL (or Java) interfaces allowing to traverse and manipulate a
698document. The DOM library will allow accessing and modifying "live" documents
699presents on other programs like this:</p>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000700
701<p><img src="DOM.gif" alt=" DOM.gif "></p>
702
703<p>This should help greatly doing things like modifying a gnumeric spreadsheet
Daniel Veillardccb09631998-10-27 06:21:04 +0000704embedded in a GWP document for example.</p>
Daniel Veillard14fff061999-06-22 21:49:07 +0000705
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000706<p>The current DOM implementation on top of libxml is the <a
707href="http://cvs.gnome.org/lxr/source/gdome/">gdome Gnome module</a>, this is
708a full DOM interface, thanks to <a href="mailto:raph@levien.com">Raph
709Levien</a>.</p>
710
711<p>The gnome-dom module in the Gnome CVS base is obsolete</p>
712
Daniel Veillard35008381999-10-25 13:15:52 +0000713<h2><a name="Example"></a><a name="real">A real example</a></h2>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000714
715<p>Here is a real size example, where the actual content of the application
716data is not kept in the DOM tree but uses internal structures. It is based on
Daniel Veillard14fff061999-06-22 21:49:07 +0000717a proposal to keep a database of jobs related to Gnome, with an XML based
Daniel Veillardb05deb71999-08-10 19:04:08 +0000718storage structure. Here is an <a href="gjobs.xml">XML encoded jobs
719base</a>:</p>
720<pre>&lt;?xml version="1.0"?>
Daniel Veillard14fff061999-06-22 21:49:07 +0000721&lt;gjob:Helping xmlns:gjob="http://www.gnome.org/some-location">
722 &lt;gjob:Jobs>
723
724 &lt;gjob:Job>
725 &lt;gjob:Project ID="3"/>
726 &lt;gjob:Application>GBackup&lt;/gjob:Application>
727 &lt;gjob:Category>Development&lt;/gjob:Category>
728
729 &lt;gjob:Update>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000730 &lt;gjob:Status>Open&lt;/gjob:Status>
731 &lt;gjob:Modified>Mon, 07 Jun 1999 20:27:45 -0400 MET DST&lt;/gjob:Modified>
Daniel Veillard14fff061999-06-22 21:49:07 +0000732 &lt;gjob:Salary>USD 0.00&lt;/gjob:Salary>
733 &lt;/gjob:Update>
734
735 &lt;gjob:Developers>
736 &lt;gjob:Developer>
737 &lt;/gjob:Developer>
738 &lt;/gjob:Developers>
739
740 &lt;gjob:Contact>
741 &lt;gjob:Person>Nathan Clemons&lt;/gjob:Person>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000742 &lt;gjob:Email>nathan@windsofstorm.net&lt;/gjob:Email>
Daniel Veillard14fff061999-06-22 21:49:07 +0000743 &lt;gjob:Company>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000744 &lt;/gjob:Company>
Daniel Veillard14fff061999-06-22 21:49:07 +0000745 &lt;gjob:Organisation>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000746 &lt;/gjob:Organisation>
Daniel Veillard14fff061999-06-22 21:49:07 +0000747 &lt;gjob:Webpage>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000748 &lt;/gjob:Webpage>
749 &lt;gjob:Snailmail>
750 &lt;/gjob:Snailmail>
751 &lt;gjob:Phone>
752 &lt;/gjob:Phone>
Daniel Veillard14fff061999-06-22 21:49:07 +0000753 &lt;/gjob:Contact>
754
755 &lt;gjob:Requirements>
756 The program should be released as free software, under the GPL.
757 &lt;/gjob:Requirements>
758
759 &lt;gjob:Skills>
760 &lt;/gjob:Skills>
761
762 &lt;gjob:Details>
763 A GNOME based system that will allow a superuser to configure
764 compressed and uncompressed files and/or file systems to be backed
765 up with a supported media in the system. This should be able to
766 perform via find commands generating a list of files that are passed
767 to tar, dd, cpio, cp, gzip, etc., to be directed to the tape machine
768 or via operations performed on the filesystem itself. Email
769 notification and GUI status display very important.
770 &lt;/gjob:Details>
771
772 &lt;/gjob:Job>
773
774 &lt;/gjob:Jobs>
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000775&lt;/gjob:Helping></pre>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000776
777<p>While loading the XML file into an internal DOM tree is a matter of calling
778only a couple of functions, browsing the tree to gather the informations and
779generate the internals structures is harder, and more error prone.</p>
780
781<p>The suggested principle is to be tolerant with respect to the input
782structure. For example the ordering of the attributes is not significant, Cthe
783XML specification is clear about it. It's also usually a good idea to not be
784dependant of the orders of the childs of a given node, unless it really makes
785things harder. Here is some code to parse the informations for a person:</p>
786<pre>/*
Daniel Veillard14fff061999-06-22 21:49:07 +0000787 * A person record
788 */
789typedef struct person {
790 char *name;
791 char *email;
792 char *company;
793 char *organisation;
794 char *smail;
795 char *webPage;
796 char *phone;
797} person, *personPtr;
798
799/*
800 * And the code needed to parse it
801 */
802personPtr parsePerson(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) {
803 personPtr ret = NULL;
804
805DEBUG("parsePerson\n");
806 /*
807 * allocate the struct
808 */
809 ret = (personPtr) malloc(sizeof(person));
810 if (ret == NULL) {
811 fprintf(stderr,"out of memory\n");
Daniel Veillardb05deb71999-08-10 19:04:08 +0000812 return(NULL);
Daniel Veillard14fff061999-06-22 21:49:07 +0000813 }
814 memset(ret, 0, sizeof(person));
815
816 /* We don't care what the top level element name is */
817 cur = cur->childs;
818 while (cur != NULL) {
819 if ((!strcmp(cur->name, "Person")) &amp;&amp; (cur->ns == ns))
Daniel Veillardb05deb71999-08-10 19:04:08 +0000820 ret->name = xmlNodeListGetString(doc, cur->childs, 1);
Daniel Veillard14fff061999-06-22 21:49:07 +0000821 if ((!strcmp(cur->name, "Email")) &amp;&amp; (cur->ns == ns))
Daniel Veillardb05deb71999-08-10 19:04:08 +0000822 ret->email = xmlNodeListGetString(doc, cur->childs, 1);
823 cur = cur->next;
Daniel Veillard14fff061999-06-22 21:49:07 +0000824 }
825
826 return(ret);
Daniel Veillardb05deb71999-08-10 19:04:08 +0000827}</pre>
828
829<p>Here is a couple of things to notice:</p>
Daniel Veillard14fff061999-06-22 21:49:07 +0000830<ul>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000831 <li>Usually a recursive parsing style is the more convenient one, XML data
832 being by nature subject to repetitive constructs and usualy exibit highly
833 stuctured patterns.</li>
834 <li>The two arguments of type <em>xmlDocPtr</em> and <em>xmlNsPtr</em>, i.e.
835 the pointer to the global XML document and the namespace reserved to the
836 application. Document wide information are needed for example to decode
837 entities and it's a good coding practice to define a namespace for your
838 application set of data and test that the element and attributes you're
839 analyzing actually pertains to your application space. This is done by a
840 simple equality test (cur->ns == ns).</li>
841 <li>To retrieve text and attributes value, it is suggested to use the
842 function <em>xmlNodeListGetString</em> to gather all the text and entity
843 reference nodes generated by the DOM output and produce an single text
844 string.</li>
Daniel Veillard14fff061999-06-22 21:49:07 +0000845</ul>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000846
847<p>Here is another piece of code used to parse another level of the
848structure:</p>
849<pre>/*
Daniel Veillard14fff061999-06-22 21:49:07 +0000850 * a Description for a Job
851 */
852typedef struct job {
853 char *projectID;
854 char *application;
855 char *category;
856 personPtr contact;
857 int nbDevelopers;
858 personPtr developers[100]; /* using dynamic alloc is left as an exercise */
859} job, *jobPtr;
860
861/*
862 * And the code needed to parse it
863 */
864jobPtr parseJob(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) {
865 jobPtr ret = NULL;
866
867DEBUG("parseJob\n");
868 /*
869 * allocate the struct
870 */
871 ret = (jobPtr) malloc(sizeof(job));
872 if (ret == NULL) {
873 fprintf(stderr,"out of memory\n");
Daniel Veillardb05deb71999-08-10 19:04:08 +0000874 return(NULL);
Daniel Veillard14fff061999-06-22 21:49:07 +0000875 }
876 memset(ret, 0, sizeof(job));
877
878 /* We don't care what the top level element name is */
879 cur = cur->childs;
880 while (cur != NULL) {
881
882 if ((!strcmp(cur->name, "Project")) &amp;&amp; (cur->ns == ns)) {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000883 ret->projectID = xmlGetProp(cur, "ID");
884 if (ret->projectID == NULL) {
885 fprintf(stderr, "Project has no ID\n");
886 }
887 }
Daniel Veillard14fff061999-06-22 21:49:07 +0000888 if ((!strcmp(cur->name, "Application")) &amp;&amp; (cur->ns == ns))
Daniel Veillardb05deb71999-08-10 19:04:08 +0000889 ret->application = xmlNodeListGetString(doc, cur->childs, 1);
Daniel Veillard14fff061999-06-22 21:49:07 +0000890 if ((!strcmp(cur->name, "Category")) &amp;&amp; (cur->ns == ns))
Daniel Veillardb05deb71999-08-10 19:04:08 +0000891 ret->category = xmlNodeListGetString(doc, cur->childs, 1);
Daniel Veillard14fff061999-06-22 21:49:07 +0000892 if ((!strcmp(cur->name, "Contact")) &amp;&amp; (cur->ns == ns))
Daniel Veillardb05deb71999-08-10 19:04:08 +0000893 ret->contact = parsePerson(doc, ns, cur);
894 cur = cur->next;
Daniel Veillard14fff061999-06-22 21:49:07 +0000895 }
896
897 return(ret);
Daniel Veillardb05deb71999-08-10 19:04:08 +0000898}</pre>
Daniel Veillard14fff061999-06-22 21:49:07 +0000899
Daniel Veillardb05deb71999-08-10 19:04:08 +0000900<p>One can notice that once used to it, writing this kind of code is quite
901simple, but boring. Ultimately, it could be possble to write stubbers taking
902either C data structure definitions, a set of XML examples or an XML DTD and
903produce the code needed to import and export the content between C data and
904XML storage. This is left as an exercise to the reader :-)</p>
905
906<p>Feel free to use <a href="gjobread.c">the code for the full C parsing
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000907example</a> as a template, it is also available with Makefile in the Gnome CVS
908base under gnome-xml/example</p>
Daniel Veillardb05deb71999-08-10 19:04:08 +0000909
Daniel Veillardc8eab3a1999-09-04 18:27:23 +0000910<p></p>
911
912<p><a href="mailto:Daniel.Veillard@w3.org">Daniel Veillard</a></p>
913
Daniel Veillard437b87b2000-01-03 17:30:46 +0000914<p>$Id: xml.html,v 1.18 1999/12/28 16:35:14 veillard Exp $</p>
Daniel Veillardccb09631998-10-27 06:21:04 +0000915</body>
916</html>