From a7e9d3f37d5e9fba4b9acaa43e7c12b6d9a669ae Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Thu, 8 Jun 2006 10:59:26 +0200 Subject: Load /tmp/libxml2-2.6.26 into libxml2/branches/upstream/current. --- doc/example.html | 70 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 36 insertions(+), 34 deletions(-) (limited to 'doc/example.html') diff --git a/doc/example.html b/doc/example.html index 894f03a..194f619 100644 --- a/doc/example.html +++ b/doc/example.html @@ -7,11 +7,11 @@ H1 {font-family: Verdana,Arial,Helvetica} H2 {font-family: Verdana,Arial,Helvetica} H3 {font-family: Verdana,Arial,Helvetica} A:link, A:visited, A:active { text-decoration: underline } -A real example
Action against software patentsGnome2 LogoW3C LogoRed Hat Logo
Made with Libxml2 Logo

The XML C parser and toolkit of Gnome

A real example

Developer Menu
API Indexes
Related links

Here is a real size example, where the actual content of the application -data is not kept in the DOM tree but uses internal structures. It is based on -a proposal to keep a database of jobs related to Gnome, with an XML based -storage structure. Here is an XML encoded jobs -base:

<?xml version="1.0"?>
+A real example
Action against software patentsGnome2 LogoW3C LogoRed Hat Logo
Made with Libxml2 Logo

The XML C parser and toolkit of Gnome

A real example

Developer Menu
API Indexes
Related links

Here is a real size example, where the actual content of +theapplicationdata is not kept in the DOM tree but uses internal structures. +Itis based ona proposal to keep a database of jobs related to Gnome, with +anXML basedstorage structure. Here is an XML +encodedjobsbase:

<?xml version="1.0"?>
 <gjob:Helping xmlns:gjob="http://www.gnome.org/some-location">
   <gjob:Jobs>
 
@@ -66,13 +66,14 @@ base:

<?xml version="1.0"?>
     </gjob:Job>
 
   </gjob:Jobs>
-</gjob:Helping>

While loading the XML file into an internal DOM tree is a matter of -calling only a couple of functions, browsing the tree to gather the data and -generate the internal structures is harder, and more error prone.

The suggested principle is to be tolerant with respect to the input -structure. For example, the ordering of the attributes is not significant, -the XML specification is clear about it. It's also usually a good idea not to -depend on the order of the children of a given node, unless it really makes -things harder. Here is some code to parse the information for a person:

/*
+</gjob:Helping>

While loading the XML file into an internal DOM tree is a matter +ofcallingonly a couple of functions, browsing the tree to gather the data +andgeneratethe internal structures is harder, and more error prone.

The suggested principle is to be tolerant with respect to +theinputstructure. For example, the ordering of the attributes is +notsignificant,the XML specification is clear about it. It's also usually a +goodidea not todepend on the order of the children of a given node, unless +itreally makesthings harder. Here is some code to parse the information for +aperson:

/*
  * A person record
  */
 typedef struct person {
@@ -113,21 +114,22 @@ DEBUG("parsePerson\n");
     }
 
     return(ret);
-}

Here are a couple of things to notice:

  • Usually a recursive parsing style is the more convenient one: XML data - is by nature subject to repetitive constructs and usually exhibits highly - structured patterns.
  • -
  • The two arguments of type xmlDocPtr and xmlNsPtr, - i.e. the pointer to the global XML document and the namespace reserved to - the application. Document wide information are needed for example to - decode entities and it's a good coding practice to define a namespace for - your application set of data and test that the element and attributes - you're analyzing actually pertains to your application space. This is - done by a simple equality test (cur->ns == ns).
  • -
  • To retrieve text and attributes value, you can use the function - xmlNodeListGetString to gather all the text and entity reference - nodes generated by the DOM output and produce an single text string.
  • -

Here is another piece of code used to parse another level of the -structure:

#include <libxml/tree.h>
+}

Here are a couple of things to notice:

  • Usually a recursive parsing style is the more convenient one: XMLdatais + by nature subject to repetitive constructs and usually + exhibitshighlystructured patterns.
  • +
  • The two arguments of type xmlDocPtrand + xmlNsPtr,i.e.the pointer to the global XML document and the + namespace reserved totheapplication. Document wide information are needed + for example todecodeentities and it's a good coding practice to define a + namespace foryourapplication set of data and test that the element and + attributesyou'reanalyzing actually pertains to your application space. + This isdone by asimple equality test (cur->ns == ns).
  • +
  • To retrieve text and attributes value, you can use + thefunctionxmlNodeListGetStringto gather all the text and + entityreferencenodes generated by the DOM output and produce an single + textstring.
  • +

Here is another piece of code used to parse another level +ofthestructure:

#include <libxml/tree.h>
 /*
  * a Description for a Job
  */
@@ -177,10 +179,10 @@ DEBUG("parseJob\n");
     }
 
     return(ret);
-}

Once you are used to it, writing this kind of code is quite simple, but -boring. Ultimately, it could be possible to write stubbers taking either C -data structure definitions, a set of XML examples or an XML DTD and produce -the code needed to import and export the content between C data and XML -storage. This is left as an exercise to the reader :-)

Feel free to use the code for the full C -parsing example as a template, it is also available with Makefile in the -Gnome CVS base under gnome-xml/example

Daniel Veillard

+}

Once you are used to it, writing this kind of code is quite +simple,butboring. Ultimately, it could be possible to write stubbers taking +eitherCdata structure definitions, a set of XML examples or an XML DTD +andproducethe code needed to import and export the content between C data +andXMLstorage. This is left as an exercise to the reader :-)

Feel free to use the code for the +fullCparsing exampleas a template, it is also available with Makefile +intheGnome CVS base under gnome-xml/example

Daniel Veillard

-- cgit v1.2.3