Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 2 | "http://www.w3.org/TR/REC-html40/loose.dtd"> |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 3 | <html> |
| 4 | <head> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 5 | <title>The XML library for Gnome</title> |
| 6 | <meta name="GENERATOR" content="amaya V2.1"> |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 7 | </head> |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 8 | |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 9 | <body bgcolor="#ffffff"> |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 10 | <h1 align="center">The XML library for Gnome</h1> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 11 | |
| 12 | <p>This document describes the <a href="http://www.w3.org/XML/">XML</a> |
| 13 | library provideed in the <a href="http://www.gnome.org/">Gnome</a> framework. |
| 14 | XML is a standard to build tag based structured documents/data. </p> |
| 15 | |
| 16 | <p>The internal document repesentation is as close as possible to the <a |
| 17 | href="http://www.w3.org/DOM/">DOM</a> interfaces. </p> |
| 18 | |
| 19 | <p>Libxml also has a <a href="http://www.megginson.com/SAX/index.html">SAX |
| 20 | interface</a>, <a href="mailto:james@daa.com.au">James Henstridge</a> made <a |
| 21 | href="http://www.daa.com.au/~james/gnome/xml-sax/xml-sax.html">a nice |
| 22 | documentation</a> expaining how to use it. The interface is as compatible as |
| 23 | possible with <a href="http://www.jclark.com/xml/expat.html">Expat</a> |
| 24 | one.</p> |
| 25 | |
| 26 | <p>The code is commented in a <a href=""></a>way which allow <a |
| 27 | href="http://rpmfind.net/veillard/XML/libxml.html">extensive documentation</a> |
| 28 | to be automatically extracted.</p> |
| 29 | |
| 30 | <p>There is also a mailing-list <a |
| 31 | href="xml@rufus.w3.org">xml@rufus.w3.org</a> for libxml, with an <a |
| 32 | href="http://rpmfind.net/veillard/XML/messages">on-line archive</a>. To |
| 33 | subscribe to this majordomo based list, send a mail to <a |
| 34 | href="majordomo@rufus.w3.org">majordomo@rufus.w3.org</a> with "subscribe xml" |
| 35 | in the <strong>content</strong> of the message.</p> |
| 36 | |
| 37 | <p>This library is released both under the W3C Copyright and the GNU LGP, |
| 38 | basically everybody should be happy, if not, drop me a mail.</p> |
| 39 | |
| 40 | <p>People are invited to use the <a |
| 41 | href="http://cvs.gnome.org/lxr/source/gdome/">gdome Gnome module to</a> get a |
| 42 | full DOM interface, thanks to <a href="mailto:raph@levien.com">Raph |
| 43 | Levien</a>, check his <a |
| 44 | href="http://www.levien.com/gnome/domination.html">DOMination paper</a>. He |
| 45 | uses it for his implementation of <a |
| 46 | href="http://www.w3.org/Graphics/SVG/">SVG</a> called <a |
| 47 | href="http://www.levien.com/svg/">gill</a>.</p> |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 48 | |
| 49 | <h2>xml</h2> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 50 | |
| 51 | <p>XML is a standard for markup based structured documents, here is <a |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 52 | name="example">an example</a>:</p> |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 53 | <pre><?xml version="1.0"?> |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 54 | <EXAMPLE prop1="gnome is great" prop2="&amp; linux too"> |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 55 | <head> |
| 56 | <title>Welcome to Gnome</title> |
| 57 | </head> |
| 58 | <chapter> |
| 59 | <title>The Linux adventure</title> |
| 60 | <p>bla bla bla ...</p> |
| 61 | <image href="linus.gif"/> |
| 62 | <p>...</p> |
| 63 | </chapter> |
| 64 | </EXAMPLE></pre> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 65 | |
| 66 | <p>The first line specify that it's an XML document and gives useful |
| 67 | informations about it's encoding. Then the document is a text format whose |
| 68 | structure is specified by tags between brackets. <strong>Each tag opened have |
| 69 | to be closed</strong> XML is pedantic about this, not that for example the |
| 70 | image tag has no content (just an attribute) and is closed by ending up the |
| 71 | tag with <code>/></code>.</p> |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 72 | |
| 73 | <h2>The tree output</h2> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 74 | |
| 75 | <p>The parser returns a tree built during the document analysis. The value |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 76 | returned is an <strong>xmlDocPtr</strong> (i.e. a pointer to an |
| 77 | <strong>xmlDoc</strong> structure). This structure contains informations like |
| 78 | the file name, the document type, and a <strong>root</strong> pointer which |
| 79 | is the root of the document (or more exactly the first child under the root |
| 80 | which is the document). The tree is made of <strong>xmlNode</strong>s, chained |
| 81 | in double linked lists of siblings and with childs<->parent relationship. |
| 82 | An xmlNode can also carry properties (a chain of xmlAttr structures). An |
| 83 | attribute may have a value which is a list of TEXT or ENTITY_REF nodes.</p> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 84 | |
| 85 | <p>Here is an example (erroneous w.r.t. the XML spec since there should be |
| 86 | only one ELEMENT under the root):</p> |
| 87 | |
| 88 | <p><img src="structure.gif" alt=" structure.gif "></p> |
| 89 | |
| 90 | <p>In the source package there is a small program (not installed by default) |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 91 | called <strong>tester</strong> which parses XML files given as argument and |
| 92 | prints them back as parsed, this is useful to detect errors both in XML code |
| 93 | and in the XML parser itself. It has an option <strong>--debug</strong> which |
| 94 | prints the actual in-memory structure of the document, here is the result with |
| 95 | the <a href="#example">example</a> given before:</p> |
| 96 | <pre>DOCUMENT |
| 97 | version=1.0 |
| 98 | standalone=true |
| 99 | ELEMENT EXAMPLE |
| 100 | ATTRIBUTE prop1 |
| 101 | TEXT |
| 102 | content=gnome is great |
| 103 | ATTRIBUTE prop2 |
| 104 | ENTITY_REF |
| 105 | TEXT |
| 106 | content= too |
| 107 | ELEMENT head |
| 108 | ELEMENT title |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 109 | TEXT |
| 110 | content=Welcome to Gnome |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 111 | ELEMENT chapter |
| 112 | ELEMENT title |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 113 | TEXT |
| 114 | content=The Linux adventure |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 115 | ELEMENT p |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 116 | TEXT |
| 117 | content=bla bla bla ... |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 118 | ELEMENT image |
| 119 | ATTRIBUTE href |
| 120 | TEXT |
| 121 | content=linus.gif |
| 122 | ELEMENT p |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 123 | TEXT |
| 124 | content=...</pre> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 125 | |
| 126 | <p>This should be useful to learn the internal representation model.</p> |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 127 | |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 128 | <h2>The XML library interfaces</h2> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 129 | |
| 130 | <p>This section is directly intended to help programmers getting bootstrapped |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 131 | using the XML library from the C language. It doesn't intent to be extensive, |
| 132 | I hope the automatically generated docs will provide the completeness |
| 133 | required, but as a separated set of documents. The interfaces of the XML |
| 134 | library are by principle low level, there is nearly zero abstration. Those |
| 135 | interested in a higher level API should <a href="#DOM">look at DOM</a> |
| 136 | (unfortunately not completed).</p> |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 137 | |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 138 | <h3>Invoking the parser</h3> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 139 | |
| 140 | <p>Usually, the first thing to do is to read an XML input, the parser accepts |
| 141 | to parse both memory mapped documents or direct files. The functions are |
| 142 | defined in "parser.h":</p> |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 143 | <dl> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 144 | <dt><code>xmlDocPtr xmlParseMemory(char *buffer, int size);</code></dt> |
| 145 | <dd><p>parse a zero terminated string containing the document</p> |
| 146 | </dd> |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 147 | </dl> |
| 148 | <dl> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 149 | <dt><code>xmlDocPtr xmlParseFile(const char *filename);</code></dt> |
| 150 | <dd><p>parse an XML document contained in a file (possibly compressed)</p> |
| 151 | </dd> |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 152 | </dl> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 153 | |
| 154 | <p>This returns a pointer to the document structure (or NULL in case of |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 155 | failure).</p> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 156 | |
| 157 | <p>A couple of comments can be made, first this mean that the parser is |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 158 | memory-hungry, first to load the document in memory, second to build the tree. |
| 159 | Reading a document without building the tree will be possible in the future by |
| 160 | pluggin the code to the SAX interface (see SAX.c).</p> |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 161 | |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 162 | <h3>Building a tree from scratch</h3> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 163 | |
| 164 | <p>The other way to get an XML tree in memory is by building it. Basically |
| 165 | there is a set of functions dedicated to building new elements, those are also |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 166 | described in "tree.h", here is for example the piece of code producing the |
| 167 | example used before:</p> |
| 168 | <pre> xmlDocPtr doc; |
| 169 | xmlNodePtr tree, subtree; |
| 170 | |
| 171 | doc = xmlNewDoc("1.0"); |
| 172 | doc->root = xmlNewDocNode(doc, NULL, "EXAMPLE", NULL); |
| 173 | xmlSetProp(doc->root, "prop1", "gnome is great"); |
| 174 | xmlSetProp(doc->root, "prop2", "&linux; too"); |
| 175 | tree = xmlNewChild(doc->root, NULL, "head", NULL); |
| 176 | subtree = xmlNewChild(tree, NULL, "title", "Welcome to Gnome"); |
| 177 | tree = xmlNewChild(doc->root, NULL, "chapter", NULL); |
| 178 | subtree = xmlNewChild(tree, NULL, "title", "The Linux adventure"); |
| 179 | subtree = xmlNewChild(tree, NULL, "p", "bla bla bla ..."); |
| 180 | subtree = xmlNewChild(tree, NULL, "image", NULL); |
| 181 | xmlSetProp(subtree, "href", "linus.gif");</pre> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 182 | |
| 183 | <p>Not really rocket science ...</p> |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 184 | |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 185 | <h3>Traversing the tree</h3> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 186 | |
| 187 | <p>Basically by including "tree.h" your code has access to the internal |
| 188 | structure of all the element of the tree. The names should be somewhat simple |
| 189 | like <strong>parent</strong>, <strong>childs</strong>, <strong>next</strong>, |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 190 | <strong>prev</strong>, <strong>properties</strong>, etc... For example still |
| 191 | with the previous example:</p> |
| 192 | <pre><code>doc->root->childs->childs</code></pre> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 193 | |
| 194 | <p>points to the title element,</p> |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 195 | <pre>doc->root->childs->next->child->child</pre> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 196 | |
| 197 | <p>points to the text node containing the chapter titlle "The Linux adventure" |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 198 | and</p> |
| 199 | <pre>doc->root->properties->next->val</pre> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 200 | |
| 201 | <p>points to the entity reference containing the value of "&linux" at the |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 202 | beginning of the second attribute of the root element "EXAMPLE".</p> |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 203 | |
| 204 | <h3>Modifying the tree</h3> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 205 | |
| 206 | <p>functions are provided to read and write the document content:</p> |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 207 | <dl> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 208 | <dt><code>xmlAttrPtr xmlSetProp(xmlNodePtr node, const CHAR *name, const |
| 209 | CHAR *value);</code></dt> |
| 210 | <dd><p>This set (or change) an attribute carried by an ELEMENT node the |
| 211 | value can be NULL</p> |
| 212 | </dd> |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 213 | </dl> |
| 214 | <dl> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 215 | <dt><code>const CHAR *xmlGetProp(xmlNodePtr node, const CHAR |
| 216 | *name);</code></dt> |
| 217 | <dd><p>This function returns a pointer to the property content, note that |
| 218 | no extra copy is made</p> |
| 219 | </dd> |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 220 | </dl> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 221 | |
| 222 | <p>Two functions must be used to read an write the text associated to |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 223 | elements:</p> |
| 224 | <dl> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 225 | <dt><code>xmlNodePtr xmlStringGetNodeList(xmlDocPtr doc, const CHAR |
| 226 | *value);</code></dt> |
| 227 | <dd><p>This function takes an "external" string and convert it to one text |
| 228 | node or possibly to a list of entity and text nodes. All non-predefined |
| 229 | entity references like &Gnome; will be stored internally as an |
| 230 | entity node, hence the result of the function may not be a single |
| 231 | node.</p> |
| 232 | </dd> |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 233 | </dl> |
| 234 | <dl> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 235 | <dt><code>CHAR *xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int |
| 236 | inLine);</code></dt> |
| 237 | <dd><p>this is the dual function, which generate a new string containing |
| 238 | the content of the text and entity nodes. Note the extra argument |
| 239 | inLine, if set to 1 instead of returning the &Gnome; XML encoding in |
| 240 | the string it will substitute it with it's value say "GNU Network Object |
| 241 | Model Environment". Set it if you want to use the string for non XML |
| 242 | usage like User Interface.</p> |
| 243 | </dd> |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 244 | </dl> |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 245 | |
| 246 | <h3>Saving a tree</h3> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 247 | |
| 248 | <p>Basically 3 options are possible:</p> |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 249 | <dl> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 250 | <dt><code>void xmlDocDumpMemory(xmlDocPtr cur, CHAR**mem, int |
| 251 | *size);</code></dt> |
| 252 | <dd><p>returns a buffer where the document has been saved</p> |
| 253 | </dd> |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 254 | </dl> |
| 255 | <dl> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 256 | <dt><code>extern void xmlDocDump(FILE *f, xmlDocPtr doc);</code></dt> |
| 257 | <dd><p>dumps a buffer to an open file descriptor</p> |
| 258 | </dd> |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 259 | </dl> |
| 260 | <dl> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 261 | <dt><code>int xmlSaveFile(const char *filename, xmlDocPtr cur);</code></dt> |
| 262 | <dd><p>save the document ot a file. In that case the compression interface |
| 263 | is triggered if turned on</p> |
| 264 | </dd> |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 265 | </dl> |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 266 | |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 267 | <h3>Compression</h3> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 268 | |
| 269 | <p>The library handle transparently compression when doing file based |
| 270 | accesses, the level of compression on saves can be tuned either globally or |
| 271 | individually for one file:</p> |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 272 | <dl> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 273 | <dt><code>int xmlGetDocCompressMode (xmlDocPtr doc);</code></dt> |
| 274 | <dd><p>Get the document compression ratio (0-9)</p> |
| 275 | </dd> |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 276 | </dl> |
| 277 | <dl> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 278 | <dt><code>void xmlSetDocCompressMode (xmlDocPtr doc, int mode);</code></dt> |
| 279 | <dd><p>Set the document compression ratio</p> |
| 280 | </dd> |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 281 | </dl> |
| 282 | <dl> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 283 | <dt><code>int xmlGetCompressMode(void);</code></dt> |
| 284 | <dd><p>Get the default compression ratio</p> |
| 285 | </dd> |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 286 | </dl> |
| 287 | <dl> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 288 | <dt><code>void xmlSetCompressMode(int mode);</code></dt> |
| 289 | <dd><p>set the default compression ratio</p> |
| 290 | </dd> |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 291 | </dl> |
| 292 | |
| 293 | <h2><a name="DOM">DOM Principles</a></h2> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 294 | |
| 295 | <p><a href="http://www.w3.org/DOM/">DOM</a> stands for the <em>Document Object |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 296 | Model</em> this is an API for accessing XML or HTML structured documents. |
| 297 | Native support for DOM in Gnome is on the way (module gnome-dom), and it will |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 298 | be based on gnome-xml. This will be a far cleaner interface to manipulate XML |
| 299 | files within Gnome since it won't expose the internal structure. DOM defiles a |
| 300 | set of IDL (or Java) interfaces allowing to traverse and manipulate a |
| 301 | document. The DOM library will allow accessing and modifying "live" documents |
| 302 | presents on other programs like this:</p> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 303 | |
| 304 | <p><img src="DOM.gif" alt=" DOM.gif "></p> |
| 305 | |
| 306 | <p>This should help greatly doing things like modifying a gnumeric spreadsheet |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 307 | embedded in a GWP document for example.</p> |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 308 | |
| 309 | <h3><a name="Example">A real example</a></h3> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 310 | |
| 311 | <p>Here is a real size example, where the actual content of the application |
| 312 | data is not kept in the DOM tree but uses internal structures. It is based on |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 313 | a proposal to keep a database of jobs related to Gnome, with an XML based |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 314 | storage structure. Here is an <a href="gjobs.xml">XML encoded jobs |
| 315 | base</a>:</p> |
| 316 | <pre><?xml version="1.0"?> |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 317 | <gjob:Helping xmlns:gjob="http://www.gnome.org/some-location"> |
| 318 | <gjob:Jobs> |
| 319 | |
| 320 | <gjob:Job> |
| 321 | <gjob:Project ID="3"/> |
| 322 | <gjob:Application>GBackup</gjob:Application> |
| 323 | <gjob:Category>Development</gjob:Category> |
| 324 | |
| 325 | <gjob:Update> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 326 | <gjob:Status>Open</gjob:Status> |
| 327 | <gjob:Modified>Mon, 07 Jun 1999 20:27:45 -0400 MET DST</gjob:Modified> |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 328 | <gjob:Salary>USD 0.00</gjob:Salary> |
| 329 | </gjob:Update> |
| 330 | |
| 331 | <gjob:Developers> |
| 332 | <gjob:Developer> |
| 333 | </gjob:Developer> |
| 334 | </gjob:Developers> |
| 335 | |
| 336 | <gjob:Contact> |
| 337 | <gjob:Person>Nathan Clemons</gjob:Person> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 338 | <gjob:Email>nathan@windsofstorm.net</gjob:Email> |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 339 | <gjob:Company> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 340 | </gjob:Company> |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 341 | <gjob:Organisation> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 342 | </gjob:Organisation> |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 343 | <gjob:Webpage> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 344 | </gjob:Webpage> |
| 345 | <gjob:Snailmail> |
| 346 | </gjob:Snailmail> |
| 347 | <gjob:Phone> |
| 348 | </gjob:Phone> |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 349 | </gjob:Contact> |
| 350 | |
| 351 | <gjob:Requirements> |
| 352 | The program should be released as free software, under the GPL. |
| 353 | </gjob:Requirements> |
| 354 | |
| 355 | <gjob:Skills> |
| 356 | </gjob:Skills> |
| 357 | |
| 358 | <gjob:Details> |
| 359 | A GNOME based system that will allow a superuser to configure |
| 360 | compressed and uncompressed files and/or file systems to be backed |
| 361 | up with a supported media in the system. This should be able to |
| 362 | perform via find commands generating a list of files that are passed |
| 363 | to tar, dd, cpio, cp, gzip, etc., to be directed to the tape machine |
| 364 | or via operations performed on the filesystem itself. Email |
| 365 | notification and GUI status display very important. |
| 366 | </gjob:Details> |
| 367 | |
| 368 | </gjob:Job> |
| 369 | |
| 370 | </gjob:Jobs> |
| 371 | </gjob:Helping> |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 372 | </pre> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 373 | |
| 374 | <p>While loading the XML file into an internal DOM tree is a matter of calling |
| 375 | only a couple of functions, browsing the tree to gather the informations and |
| 376 | generate the internals structures is harder, and more error prone.</p> |
| 377 | |
| 378 | <p>The suggested principle is to be tolerant with respect to the input |
| 379 | structure. For example the ordering of the attributes is not significant, Cthe |
| 380 | XML specification is clear about it. It's also usually a good idea to not be |
| 381 | dependant of the orders of the childs of a given node, unless it really makes |
| 382 | things harder. Here is some code to parse the informations for a person:</p> |
| 383 | <pre>/* |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 384 | * A person record |
| 385 | */ |
| 386 | typedef struct person { |
| 387 | char *name; |
| 388 | char *email; |
| 389 | char *company; |
| 390 | char *organisation; |
| 391 | char *smail; |
| 392 | char *webPage; |
| 393 | char *phone; |
| 394 | } person, *personPtr; |
| 395 | |
| 396 | /* |
| 397 | * And the code needed to parse it |
| 398 | */ |
| 399 | personPtr parsePerson(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) { |
| 400 | personPtr ret = NULL; |
| 401 | |
| 402 | DEBUG("parsePerson\n"); |
| 403 | /* |
| 404 | * allocate the struct |
| 405 | */ |
| 406 | ret = (personPtr) malloc(sizeof(person)); |
| 407 | if (ret == NULL) { |
| 408 | fprintf(stderr,"out of memory\n"); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 409 | return(NULL); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 410 | } |
| 411 | memset(ret, 0, sizeof(person)); |
| 412 | |
| 413 | /* We don't care what the top level element name is */ |
| 414 | cur = cur->childs; |
| 415 | while (cur != NULL) { |
| 416 | if ((!strcmp(cur->name, "Person")) && (cur->ns == ns)) |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 417 | ret->name = xmlNodeListGetString(doc, cur->childs, 1); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 418 | if ((!strcmp(cur->name, "Email")) && (cur->ns == ns)) |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 419 | ret->email = xmlNodeListGetString(doc, cur->childs, 1); |
| 420 | cur = cur->next; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | return(ret); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 424 | }</pre> |
| 425 | |
| 426 | <p>Here is a couple of things to notice:</p> |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 427 | <ul> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 428 | <li>Usually a recursive parsing style is the more convenient one, XML data |
| 429 | being by nature subject to repetitive constructs and usualy exibit highly |
| 430 | stuctured patterns.</li> |
| 431 | <li>The two arguments of type <em>xmlDocPtr</em> and <em>xmlNsPtr</em>, i.e. |
| 432 | the pointer to the global XML document and the namespace reserved to the |
| 433 | application. Document wide information are needed for example to decode |
| 434 | entities and it's a good coding practice to define a namespace for your |
| 435 | application set of data and test that the element and attributes you're |
| 436 | analyzing actually pertains to your application space. This is done by a |
| 437 | simple equality test (cur->ns == ns).</li> |
| 438 | <li>To retrieve text and attributes value, it is suggested to use the |
| 439 | function <em>xmlNodeListGetString</em> to gather all the text and entity |
| 440 | reference nodes generated by the DOM output and produce an single text |
| 441 | string.</li> |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 442 | </ul> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 443 | |
| 444 | <p>Here is another piece of code used to parse another level of the |
| 445 | structure:</p> |
| 446 | <pre>/* |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 447 | * a Description for a Job |
| 448 | */ |
| 449 | typedef struct job { |
| 450 | char *projectID; |
| 451 | char *application; |
| 452 | char *category; |
| 453 | personPtr contact; |
| 454 | int nbDevelopers; |
| 455 | personPtr developers[100]; /* using dynamic alloc is left as an exercise */ |
| 456 | } job, *jobPtr; |
| 457 | |
| 458 | /* |
| 459 | * And the code needed to parse it |
| 460 | */ |
| 461 | jobPtr parseJob(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) { |
| 462 | jobPtr ret = NULL; |
| 463 | |
| 464 | DEBUG("parseJob\n"); |
| 465 | /* |
| 466 | * allocate the struct |
| 467 | */ |
| 468 | ret = (jobPtr) malloc(sizeof(job)); |
| 469 | if (ret == NULL) { |
| 470 | fprintf(stderr,"out of memory\n"); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 471 | return(NULL); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 472 | } |
| 473 | memset(ret, 0, sizeof(job)); |
| 474 | |
| 475 | /* We don't care what the top level element name is */ |
| 476 | cur = cur->childs; |
| 477 | while (cur != NULL) { |
| 478 | |
| 479 | if ((!strcmp(cur->name, "Project")) && (cur->ns == ns)) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 480 | ret->projectID = xmlGetProp(cur, "ID"); |
| 481 | if (ret->projectID == NULL) { |
| 482 | fprintf(stderr, "Project has no ID\n"); |
| 483 | } |
| 484 | } |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 485 | if ((!strcmp(cur->name, "Application")) && (cur->ns == ns)) |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 486 | ret->application = xmlNodeListGetString(doc, cur->childs, 1); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 487 | if ((!strcmp(cur->name, "Category")) && (cur->ns == ns)) |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 488 | ret->category = xmlNodeListGetString(doc, cur->childs, 1); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 489 | if ((!strcmp(cur->name, "Contact")) && (cur->ns == ns)) |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 490 | ret->contact = parsePerson(doc, ns, cur); |
| 491 | cur = cur->next; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | return(ret); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 495 | }</pre> |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 496 | |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame^] | 497 | <p>One can notice that once used to it, writing this kind of code is quite |
| 498 | simple, but boring. Ultimately, it could be possble to write stubbers taking |
| 499 | either C data structure definitions, a set of XML examples or an XML DTD and |
| 500 | produce the code needed to import and export the content between C data and |
| 501 | XML storage. This is left as an exercise to the reader :-)</p> |
| 502 | |
| 503 | <p>Feel free to use <a href="gjobread.c">the code for the full C parsing |
| 504 | example</a> as a template,</p> |
| 505 | |
| 506 | <p> <a href="mailto:Daniel.Veillard@w3.org">Daniel Veillard</a></p> |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 507 | </body> |
| 508 | </html> |