Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

lxml.c File Reference

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include "lxml.h"

Include dependency graph for lxml.c:

Include dependency graph

Compounds

struct  LXMLattr_s
struct  LXMLelement_s
struct  LXMLhandle_s

Defines

#define LXML_XML_VERSION_1_0   "xml version = \"1.0\""
#define LXML_INDENT_STRING   " "

Typedefs

typedef LXMLattr_s LXMLattr

Functions

LXMLhandlelxml_new (void)
void lxml_free (LXMLhandle *handle)
int lxml_set_filename (LXMLhandle *handle, const char *filename)
int lxml_set_encoding (LXMLhandle *handle, const char *encoding)
const char * lxml_get_encoding (LXMLhandle *handle)
int lxml_set_dtd (LXMLhandle *handle, const char *dtd)
const char * lxml_get_dtd (LXMLhandle *handle)
int lxml_file_open (LXMLhandle *handle, int mode)
int lxml_file_close (LXMLhandle *handle)
cntreelxml_file_read (LXMLhandle *handle)
int lxml_file_write (LXMLhandle *handle, cntree *ntree)
cntreelxml_tree_new (void)
void lxml_tree_free (cntree *ntree)
void lxml_tree_free_structural_data (cntree *ntree)
LXMLelementlxml_tree_get_element (cntree *atree)
int lxml_tree_for_each_element_in_path (cntree *ntree, char **path, int(*callback)(cntree *atree, void *adata, int aop), void *data, int op)
int lxml_tree_convert (cntree *ntree, LXMLelement_callbacks *element_cb, int op)
int lxml_element_name (LXMLelement *element, char *name, int op)
int lxml_element_store_attr (LXMLelement *element, LXMLstore *store, int op)
int lxml_element_attr_count (LXMLelement *element)

Define Documentation

#define LXML_INDENT_STRING   " "
 

#define LXML_XML_VERSION_1_0   "xml version = \"1.0\""
 


Typedef Documentation

typedef struct LXMLattr_s LXMLattr
 


Function Documentation

int lxml_element_attr_count LXMLelement element  ) 
 

Return the number of attributes of the given element

Parameters:
element the element which attributes are to be counted
Returns:
the number of attributes of the element

int lxml_element_name LXMLelement element,
char *  name,
int  op
 

Check or set the name of an element

Parameters:
element the element which name is to be checked or set
name the name to compare with or to set
op LXML_Read to check if the element has the given name, or LXML_Write to set the name of the element to the given name
Returns:
0 on success (element name matches or successfully set), -1 on error (element name did not match or not set successfully).

int lxml_element_store_attr LXMLelement element,
LXMLstore store,
int  op
 

Store the attributes of an element Together with lxml_convert_tree(), this is second most important/used function in LXML. It is used to convert between the XML attributes of an element and the application specific data representation. For this task, it uses the LXMLstore structure that describes the attributes and where their values are stored in the application specific data representation. This function is typically used from callbacks called by lxml_tree_convert(). When converting from an XML tree (op flag set to LXML_Read), we need to use the attributes values of an element to store them into the application specific data representation. In this case, this function will check the attributes of the element and if it finds an attribute matching one listed in the 'store' LXMLstore array, it convert it according to the LXMLstore::value_type and store it in the associated pointer LXMLstore::dest. If the LXMLstore::dest_type is of type LXML_List, then the value is stored in the list pointed to by LXMLstore::dest. When converting to an XML tree (op flag set to LXML_Write), this function iterates through the array 'store', and if LXMLstore::dest is not NULL and if *LXMLstore::dest is also not NULL, then a new attribute is created with the value of *(LXMLstore::dest) converted to string. The new attribute is then appended to the list of attributes of the element. If the LXMLstore::dest_type is of type LXML_List, then the list traversed and for each data a new attribute is created and appended.

Parameters:
element the element that contain the attributes
store a NULL terminated array of LXMLstore describing the mapping between attributes and application specific data representation
op the conversion direction flag. LXML_Read to convert from attributes, and LXML_Write to convert to attributes
Returns:
0 on success and -1 on error

int lxml_file_close LXMLhandle handle  ) 
 

Close the XML file If the file was opened in LXML_Read mode, it is munmap'ed.

Parameters:
handle the LXML handle
Returns:
0 on success or -1 on error.

int lxml_file_open LXMLhandle handle,
int  mode
 

Open the XML file for reading or writing The file name must have been set with lxml_set_filename() before calling this function

Parameters:
handle the LXML handle
mode LXML_Read or LXML_Write
Returns:
0 on success or -1 on error.

cntree* lxml_file_read LXMLhandle handle  ) 
 

Read the XML file and convert it to a tree structure The tree structure consist of a tree of nodes. Each node represents an element of the XML file. The tree is a cntree (see cntree.h). Each node points to the associated element and contains a list of its children along with a pointer to its parent. The element is of type LXMLElement and contains the name of the element, a flag to inform whether it has children elements and a list of its attributes.

Parameters:
handle the LXML handle representing the file to read. Before using lxml_file_read(), lxml_file_open() must have been called with the LXML_Read mode
Returns:
the cntree representing the XML tree, or NULL on error.

int lxml_file_write LXMLhandle handle,
cntree ntree
 

Write an XML tree representing the given tree If the encoding was previously set with lxml_set_encoding(), it will be written with the prologue of the XML file. If the DTD was previously set with lxml_set_dtd(), then the document type declaration will be written.

Parameters:
handle the LXML handle representing the file to write. Before using lxml_file_write(), lxml_file_open() must have been called with the LXML_Write mode
ntree the cntree representing an XML tree. The data of each node must be a pointer to an LXMLelement.
Returns:
0 on success and -1 on error.

void lxml_free LXMLhandle handle  ) 
 

Free the ressources used by the handle. If lxml_file_open() was called before, you need to call lxml_file_close() yourself

Parameters:
handle the handle created by lxml_new()

const char* lxml_get_dtd LXMLhandle handle  ) 
 

Return the DTD as read from the file prologue The file must be read with lxml_file_read() before using this function. FIXME: not implemented yet

Parameters:
handle the LXML handle
Returns:
the DTD of the file, as read by lxml_file_read() or as set by lxml_set_dtd. The former case is not implemented yet.

const char* lxml_get_encoding LXMLhandle handle  ) 
 

Return the encoding as read from the file prologue The file must be read with lxml_file_read() before using this function. FIXME: not implemented yet

Parameters:
handle the LXML handle
Returns:
the encoding of the file, as read by lxml_file_read() or as set by lxml_set_encoding. The former case is not implemented yet.

LXMLhandle* lxml_new void   ) 
 

Create a new handle to access an XML file

Returns:
a newly allocated LXMLhandle to be freeed with lxml_free()

int lxml_set_dtd LXMLhandle handle,
const char *  dtd
 

Set the DTD of the XML file This is only used in the prolog of the file. This corresponds to a SYSTEM DTD.

Parameters:
handle the LXML handle
dtd the DTD to be used in the XML prologue
Returns:
0 on success, -1 on memory error

int lxml_set_encoding LXMLhandle handle,
const char *  encoding
 

Set the encoding of the XML file This is only used in the prolog of the file. No conversion is done by LXML. Strings are written as they are. FIXME: quotes should be escaped.

Parameters:
handle the LXML handle
encoding the encoding to be used in the XML prologue
Returns:
0 on success, -1 on memory error

int lxml_set_filename LXMLhandle handle,
const char *  filename
 

Set the name of the XML file

Parameters:
handle the LXML handle
filename the name of the file
Returns:
0 on success, -1 on memory error

int lxml_tree_convert cntree ntree,
LXMLelement_callbacks element_cb,
int  op
 

Convert between an XML element and application specific data. This function is to be called when convertin from or to an XML tree, depending on the 'op' flag (resp. LXML_Read or LXML_Write). This is the main function of LXML. It is passed a structure containing pointers to callback functions responsible for the actual conversion. When converting from an XML tree (LXML_Read), after an XML file was read, the callbacks functions listed in 'element_cb' are called whenever a corresponding element is matched. The callback function is called with the following arguments: the associated tree node, an associated, a user provided pointer to a pointer, a pointer to NULL and the LXML_Read flag. The callback is responsible to create the application data associated to the element and to initialize it, eventually by using the attributes of the element. When converting to an XML tree, an initial root element (tree) must be provided by the user. That element must have its name set to the root element of the file. Then for each 'element_cb' data that is non NULL or that points to a non-NULL pointer, the associated user callback is called with the following arguments: a newly created tree node with the associated element name, the associated LXMLelement_callbacks::data, a pointer to a pointer pointing initially to NULL and meant to be used by the callback function as an iterator, and the LXML_Write flag. The 'data_iter' is initially set to NULL for each element. The callback function can therefore detect that it was called for the first time. If the application specific data is not a single data but a container, the callback function sets the pointer pointed to be 'data_iter' to the start iterator associated with the container. It then processes the data pointed to by the iterator, then it advances the iterator. If the iterator reaches the end of the container, it must be set to NULL. This is because LXML will continously call the same callback when it detects that the pointer pointed to by data_iter is non NULL, which means that there are still nodes to be created with the same element at that same depth.

Parameters:
ntree a cntree representing an XML tree. The element name must be already set
element_cb an array of LXMLelement_callbacks used to convert between XML elements and application specific data
op a flag representing the direction of the conversion: LXML_Read when converting from an XML tree, and LXML_Write when converting to an XML tree
Returns:
0 on success and -1 on failure

int lxml_tree_for_each_element_in_path cntree ntree,
char **  path,
int(*  callback)(cntree *atree, void *adata, int aop),
void *  data,
int  op
 

For each node referenced by the given path, call a user provided callback. This is currently not used. This functions traverses the XML tree and for each path matching the given path, it calls the user provided callback.

Parameters:
ntree a cntree representing an XML tree. Each node of the tree must be a pointer to an LXMLelement.
path a NULL terminated array of element names representing the path to be matched
callback the user callback to call for each matching element
data the user provided data to pass to the callback function
op the conversion direction (LXML_Read or LXML_Write)
Returns:
0 on success, -1 on error.

void lxml_tree_free cntree ntree  ) 
 

Free an XML tree Every resources used by the tree is freeed.

Parameters:
ntree a cntree representing an XML tree. Each node of the tree must be a pointer to an LXMLelement.

Here is the call graph for this function:

void lxml_tree_free_structural_data cntree ntree  ) 
 

Free the XML tree but do not the attribute values Currently not used. Deprecated.

Parameters:
ntree a cntree representing an XML tree. Each node of the tree must be a pointer to an LXMLelement.

Here is the call graph for this function:

LXMLelement* lxml_tree_get_element cntree ntree  ) 
 

Get the element associated to the current tree

Parameters:
ntree a cntree representing an XML tree. Each node of the tree must be a pointer to an LXMLelement.
Returns:
the element pointed by the tree node ntree

Here is the call graph for this function:

cntree* lxml_tree_new void   ) 
 

Create an empty tree with a single unnamed root element

Returns:
a newly allocated cntree which node is a pointer to an unname root element (LXMLelement)

Here is the call graph for this function:


Generated on Wed Oct 22 15:53:08 2003 for XetPan by doxygen 1.3.2