Re: Gnome-Bugs and SMTP



Well, as you probably know, XML is based on SGML and HTML is another
example of SGML.  If an XML document is well formed, all tags should be
closed.  For example:
  <something id="hello">
    <somethingelse>Text</somethingelse>
    <somethingelse/>
  </something>

would be well formed.  The embedding of the elements (or tags, if you
prefer) forms a tree structure:
  something
    somethingelse
      Text  (this is a text node in the tree)
    somethingelse

The tree API allows you to process an XML document as if it is a tree.
You would start by using xmlParseFile() to create an xmlDocPtr structure.
When you are just starting, the only field in the structure you have to
worry about is root, which is an xmlNodePtr corresponding to the root
element in the tree (in this case, the something element).

When starting out, the only fields of the xmlNodePtr structure you need to
worry about are name, parent, next, prev and childs.  The name filed is
the string name of this element.  The next and prev fields point to the
next/previous elements at this level in the tree.  The childs field points
to the first child element.  Any of these three can be NULL if there is no
such node.

Attributes are the extra name/value pairs that occur inside the starting
tag for an element.  In the example, id is an attribute of the something
element, and hello is the value of the attribute.  You can get the values
attributes for an element with xmlGetProp(), which returns a newly
malloc'd string (which should be freed with free rather than g_free) or
NULL.

To get the content inside a node (such as the first somethingelse element
in the example), use xmlNodeGetContent.  You need to free() this
function's result as well.

Some of the code you see also use xml namespaces.  If you read some of the
xml namespace spec at www.w3.org you should be able to understand what the
code is doing (it is not particularly difficult, so it may be obvious to 
you).

Using libxml to save an XML file is the reverse of this.  You build up a
complete document tree using the various commands in the library and then
use xmlSaveFile to write the tree out in XML format.

This should help you get started.  If you want, you might want to write a
tutorial on getting started with libxml based on your experiences.  It
could be added to the tutorials currently on developer.gnome.org.

James.

--
Email: james@daa.com.au
WWW:   http://www.daa.com.au/~james/


On Wed, 10 Nov 1999, Ali Abdin wrote:

> I've looked at all those :) I've read all the documentation (except 
> source code) that is out there on libxml (mainly the docs with libxml and 
> your SAXParser thingy).
> 
> Anyway it is very 'confusing' cos I'm just trying to understand XML - For 
> example - is everything based on a 'tree' system? Whats the difference 
> between an attribute and an element? What's an entity - is it necessary? 
> What are properties? These things are very confusing to somebody whos 
> never touched HTML/SGML/XML.
> 
> I think there needs to be some sort of 'introductory tutorial' on it :)
> 
> 
> I'm more interested at the moment in /reading/ a file.
> 
> I'd appreciate it if you could awnser the above questions or point me in 
> the right direction :)
> 
> On Wed, 10 Nov 1999, James Henstridge wrote:
> 
> > There is heaps of sample code for reading/writing files in the gnome CVS
> > tree.  Gnumeric and dia are good examples of reading and writing XML files
> > using the tree API.  libglade provides an example of the SAX like API.
> > There is also API docs at:
> >   http://rufus.w3.org/veillard/XML/xml.html
> > 
> > James.
> > 
> > --
> > Email: james@daa.com.au
> > WWW:   http://www.daa.com.au/~james/
> > 
> > 
> > On Wed, 10 Nov 1999, Ali Abdin wrote:
> > 
> > > I'm still looking into this as I'm trying to teach myself XML and how to 
> > > use libxml (wish there was some sort of 'tutorial' or 'sample program' 
> > > that uses XML without a DTD)
> > > 
> > 
> > 
> 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]