Tizen Native API
Simple_XML

This is a simplistic relaxed SAX-like XML parser.

This parser is far from being compliant with XML standards, but works for most XMLs out there. If you know that your format is simple and won't vary in the future with strange corner cases, then you can use it safely.

The parser is SAX like, that is, it tokenizes content and calls you back so that you can take some action. No content is allocated during this parser work and it's not recursive, so you can use it with a very large document without any issues.

It does not validate the document anyhow, neither does it create a tree hierarchy. That's up to you.

Accordingly to XML, open tags may contain attributes. This parser does not tokenize this. If you want you can use eina_simple_xml_tag_attributes_find() and then eina_simple_xml_attributes_parse().

Functions

Eina_Bool eina_simple_xml_parse (const char *buf, unsigned buflen, Eina_Bool strip, Eina_Simple_XML_Cb func, const void *data)
 Parses a section of the XML string text.
const char * eina_simple_xml_tag_attributes_find (const char *buf, unsigned buflen)
 Finds where the attributes start from given the contents of a tag are provided.
Eina_Bool eina_simple_xml_attributes_parse (const char *buf, unsigned buflen, Eina_Simple_XML_Attribute_Cb func, const void *data)
 Parses a buffer with XML attributes to key=value pairs.
Eina_Simple_XML_Attribute * eina_simple_xml_attribute_new (Eina_Simple_XML_Node_Tag *parent, const char *key, const char *value)
 Creates (and appends) a new attribute to the tag.
void eina_simple_xml_attribute_free (Eina_Simple_XML_Attribute *attr)
 Removes an attribute from the parent and deletes it.
Eina_Simple_XML_Node_Tag * eina_simple_xml_node_tag_new (Eina_Simple_XML_Node_Tag *parent, const char *name)
 Creates a new tag. If parent is provided, it is automatically appended.
void eina_simple_xml_node_tag_free (Eina_Simple_XML_Node_Tag *tag)
 Removes a tag from the parent and deletes it.
Eina_Simple_XML_Node_Data * eina_simple_xml_node_data_new (Eina_Simple_XML_Node_Tag *parent, const char *contents, size_t length)
 Creates new data. If parent is provided, it is automatically appended.
void eina_simple_xml_node_data_free (Eina_Simple_XML_Node_Data *node)
 Removes data from the parent and deletes it.
Eina_Simple_XML_Node_CData * eina_simple_xml_node_cdata_new (Eina_Simple_XML_Node_Tag *parent, const char *contents, size_t length)
 Creates new cdata. If parent is provided, it is automatically appended.
void eina_simple_xml_node_cdata_free (Eina_Simple_XML_Node_Data *node)
 Removes cdata from the parent and deletes it.
Eina_Simple_XML_Node_Doctype_Childeina_simple_xml_node_doctype_child_new (Eina_Simple_XML_Node_Tag *parent, const char *contents, size_t length)
 Creates a new doctype child. If parent is provided, it is automatically appended.
void eina_simple_xml_node_doctype_child_free (Eina_Simple_XML_Node_Data *node)
 Removes the doctype child from the parent and deletes it.
Eina_Simple_XML_Node_Processing * eina_simple_xml_node_processing_new (Eina_Simple_XML_Node_Tag *parent, const char *contents, size_t length)
 Creates new processing. If parent is provided, it is automatically appended.
void eina_simple_xml_node_processing_free (Eina_Simple_XML_Node_Data *node)
 Removes processing from the parent and deletes it.
Eina_Simple_XML_Node_Doctype * eina_simple_xml_node_doctype_new (Eina_Simple_XML_Node_Tag *parent, const char *contents, size_t length)
 Creates a new doctype. If parent is provided, it is automatically appended.
void eina_simple_xml_node_doctype_free (Eina_Simple_XML_Node_Data *node)
 Removes the doctype from the parent and deletes it.
Eina_Simple_XML_Node_Comment * eina_simple_xml_node_comment_new (Eina_Simple_XML_Node_Tag *parent, const char *contents, size_t length)
 Creates a new comment. If parent is provided, it is automatically appended.
void eina_simple_xml_node_comment_free (Eina_Simple_XML_Node_Data *node)
 Removes a comment from the parent and deletes it.
Eina_Simple_XML_Node_Root * eina_simple_xml_node_load (const char *buf, unsigned buflen, Eina_Bool strip)
 Loads an XML node tree based on the given string.
void eina_simple_xml_node_root_free (Eina_Simple_XML_Node_Root *root)
 Frees the node tree built with eina_simple_xml_node_load().
char * eina_simple_xml_node_dump (Eina_Simple_XML_Node *node, const char *indent)
 Converts the node tree under the given element to an XML string.

Typedefs

typedef struct
_Eina_Simple_XML_Node_Data 
Eina_Simple_XML_Node_Doctype_Child

Typedef Documentation

typedef struct _Eina_Simple_XML_Node_Data Eina_Simple_XML_Node_Doctype_Child
Since (EFL) :
1.8

Enumeration Type Documentation

Enumeration for a simple XML node type.

Enumerator:
EINA_SIMPLE_XML_NODE_DOCTYPE_CHILD 
Since (EFL) :
1.8

Enumeration for a simple XML type.

Enumerator:
EINA_SIMPLE_XML_OPEN 

<tag attribute="value">

EINA_SIMPLE_XML_OPEN_EMPTY 

<tag attribute="value" />

EINA_SIMPLE_XML_CLOSE 

</tag>

EINA_SIMPLE_XML_DATA 

tag text data

EINA_SIMPLE_XML_CDATA 

<![CDATA[something]]>

EINA_SIMPLE_XML_ERROR 

Error contents

EINA_SIMPLE_XML_PROCESSING 

<?xml ... ?> <?php .. ?>

EINA_SIMPLE_XML_DOCTYPE 

<!DOCTYPE html

EINA_SIMPLE_XML_COMMENT 

<!-- something -->

EINA_SIMPLE_XML_IGNORED 

Whatever is ignored by the parser, like whitespace

EINA_SIMPLE_XML_DOCTYPE_CHILD 

<!DOCTYPE_CHILD

Since (EFL) :
1.8

Function Documentation

void eina_simple_xml_attribute_free ( Eina_Simple_XML_Attribute *  attr)

Removes an attribute from the parent and deletes it.

Since :
2.3.1
Parameters:
[in]attrThe attribute to release memory of
Eina_Simple_XML_Attribute* eina_simple_xml_attribute_new ( Eina_Simple_XML_Node_Tag *  parent,
const char *  key,
const char *  value 
)

Creates (and appends) a new attribute to the tag.

Since :
2.3.1
Parameters:
[in]parentIf provided, this is set in the resulting structure and the attribute is appended to the attributes list
[in]keyThe null-terminated string
Must not be NULL.
[in]valueThe null-terminated string
If NULL, the empty string is used.
Returns:
The newly allocated memory, otherwise NULL on error
This memory should be released directly with eina_simple_xml_attribute_free() or indirectly with eina_simple_xml_node_tag_free().
Eina_Bool eina_simple_xml_attributes_parse ( const char *  buf,
unsigned  buflen,
Eina_Simple_XML_Attribute_Cb  func,
const void *  data 
)

Parses a buffer with XML attributes to key=value pairs.

Since :
2.3.1
Parameters:
[in]bufThe input string
May not contain \0 terminator.
[in]buflenThe input string size
[in]funcThe function to call back while parse does some action
The first parameter is the given user data, the second is the key (null-terminated) and the last is the value (null terminated)
These strings should not be modified and the reference is just valid till the function returns
[in]dataThe data to pass to the callback function
Returns:
EINA_TRUE on success, otherwise EINA_FALSE if it is aborted by the user or a parsing error occurs
void eina_simple_xml_node_cdata_free ( Eina_Simple_XML_Node_Data *  node)

Removes cdata from the parent and deletes it.

Since :
2.3.1
Parameters:
[in]nodeThe cdata to release memory of
Eina_Simple_XML_Node_CData* eina_simple_xml_node_cdata_new ( Eina_Simple_XML_Node_Tag *  parent,
const char *  contents,
size_t  length 
)

Creates new cdata. If parent is provided, it is automatically appended.

Since :
2.3.1
Parameters:
[in]parentIf provided, this is set in the resulting structure and the cdata is appended to the children list
[in]contentsThe string to be used
Must not be NULL.
[in]lengthsize in bytes of contents
Returns:
The newly allocated memory, otherwise NULL on error
This memory of the parent should be released directly with eina_simple_xml_node_cdata_free() or indirectly with eina_simple_xml_node_tag_free().
void eina_simple_xml_node_comment_free ( Eina_Simple_XML_Node_Data *  node)

Removes a comment from the parent and deletes it.

Since :
2.3.1
Parameters:
[in]nodeThe comment to release memory of
Eina_Simple_XML_Node_Comment* eina_simple_xml_node_comment_new ( Eina_Simple_XML_Node_Tag *  parent,
const char *  contents,
size_t  length 
)

Creates a new comment. If parent is provided, it is automatically appended.

Since :
2.3.1
Parameters:
[in]parentIf provided, this is set in the resulting structure and the comment is appended to the children list
[in]contentsThe string to be used
Must not be NULL.
[in]lengthsize in bytes of contents
Returns:
The newly allocated memory, otherwise NULL on error
This memory of the parent should be released directly with eina_simple_xml_node_comment_free() or indirectly with eina_simple_xml_node_tag_free().
void eina_simple_xml_node_data_free ( Eina_Simple_XML_Node_Data *  node)

Removes data from the parent and deletes it.

Since :
2.3.1
Parameters:
[in]nodeThe data to release memory of
Eina_Simple_XML_Node_Data* eina_simple_xml_node_data_new ( Eina_Simple_XML_Node_Tag *  parent,
const char *  contents,
size_t  length 
)

Creates new data. If parent is provided, it is automatically appended.

Since :
2.3.1
Parameters:
[in]parentIf provided, this is set in the resulting structure and the data is appended to the children list
[in]contentsThe string to be used
Must not be NULL.
[in]lengthsize in bytes of contents
Returns:
The newly allocated memory, otherwise NULL on error
This memory of the parent should be released directly with eina_simple_xml_node_data_free() or indirectly with eina_simple_xml_node_tag_free().
void eina_simple_xml_node_doctype_child_free ( Eina_Simple_XML_Node_Data *  node)

Removes the doctype child from the parent and deletes it.

Since (EFL) :
1.8
Since :
2.3.1
Parameters:
[in]nodeThe doctype child to release memory of
Eina_Simple_XML_Node_Doctype_Child* eina_simple_xml_node_doctype_child_new ( Eina_Simple_XML_Node_Tag *  parent,
const char *  contents,
size_t  length 
)

Creates a new doctype child. If parent is provided, it is automatically appended.

Since (EFL) :
1.8
Since :
2.3.1
Parameters:
[in]parentIf provided, this is set in the resulting structure and the doctype child is appended to the children list
[in]contentsThe string to be used
Must not be NULL.
[in]lengthsize in bytes of contents
Returns:
The newly allocated memory, otherwise NULL on error
This memory of the parent should be released directly with eina_simple_xml_node_doctype_child_free() or indirectly with eina_simple_xml_node_tag_free().
void eina_simple_xml_node_doctype_free ( Eina_Simple_XML_Node_Data *  node)

Removes the doctype from the parent and deletes it.

Since :
2.3.1
Parameters:
[in]nodeThe doctype to release memory of
Eina_Simple_XML_Node_Doctype* eina_simple_xml_node_doctype_new ( Eina_Simple_XML_Node_Tag *  parent,
const char *  contents,
size_t  length 
)

Creates a new doctype. If parent is provided, it is automatically appended.

Since :
2.3.1
Parameters:
[in]parentIf provided, this is set in the resulting structure and the doctype is appended to the children list
[in]contentsThe string to be used
Must not be NULL.
[in]lengthsize in bytes of contents
Returns:
The newly allocated memory, otherwise NULL on error
This memory of the parent should be released directly with eina_simple_xml_node_doctype_free() or indirectly with eina_simple_xml_node_tag_free().
char* eina_simple_xml_node_dump ( Eina_Simple_XML_Node *  node,
const char *  indent 
)

Converts the node tree under the given element to an XML string.

Since :
2.3.1
Parameters:
[in]nodeThe base node to convert
[in]indentThe indentation string, otherwise NULL to disable it
Returns:
NULL on errors, otherwise a newly allocated string on success
Eina_Simple_XML_Node_Root* eina_simple_xml_node_load ( const char *  buf,
unsigned  buflen,
Eina_Bool  strip 
)

Loads an XML node tree based on the given string.

Since :
2.3.1
Parameters:
[in]bufThe input string
May not contain \0 terminator.
[in]buflenThe input string size
[in]stripThe boolean value that indicates when this parser should do strip leading and whitespace trailing
Returns:
The document root with children tags, otherwise NULL on errors
The document with errors may return a partial tree instead of NULL, we are going to do our best to avoid returning nothing.
void eina_simple_xml_node_processing_free ( Eina_Simple_XML_Node_Data *  node)

Removes processing from the parent and deletes it.

Since :
2.3.1
Parameters:
[in]nodeThe processing to release memory of
Eina_Simple_XML_Node_Processing* eina_simple_xml_node_processing_new ( Eina_Simple_XML_Node_Tag *  parent,
const char *  contents,
size_t  length 
)

Creates new processing. If parent is provided, it is automatically appended.

Since :
2.3.1
Parameters:
[in]parentIf provided, this is set in the resulting structure and the processing is appended to the children list
[in]contentsThe string to be used
Must not be NULL.
[in]lengthsize in bytes of contents
Returns:
The newly allocated memory, otherwise NULL on error
This memory of the parent should be released directly with eina_simple_xml_node_processing_free() or indirectly with eina_simple_xml_node_tag_free().
void eina_simple_xml_node_root_free ( Eina_Simple_XML_Node_Root *  root)

Frees the node tree built with eina_simple_xml_node_load().

Since :
2.3.1
Parameters:
[in]rootThe memory returned by eina_simple_xml_node_load()
void eina_simple_xml_node_tag_free ( Eina_Simple_XML_Node_Tag *  tag)

Removes a tag from the parent and deletes it.

Since :
2.3.1
Parameters:
[in]tagThe tag to release memory of
Eina_Simple_XML_Node_Tag* eina_simple_xml_node_tag_new ( Eina_Simple_XML_Node_Tag *  parent,
const char *  name 
)

Creates a new tag. If parent is provided, it is automatically appended.

Since :
2.3.1
Parameters:
[in]parentIf provided, this is set in the resulting structure and the tag is appended to the children list
[in]nameThe null-terminated string
Must not be NULL.
Returns:
The newly allocated memory, otherwise NULL on error
This memory of the parent should be released directly with eina_simple_xml_node_tag_free() or indirectly with eina_simple_xml_node_tag_free().
Eina_Bool eina_simple_xml_parse ( const char *  buf,
unsigned  buflen,
Eina_Bool  strip,
Eina_Simple_XML_Cb  func,
const void *  data 
)

Parses a section of the XML string text.

Since :
2.3.1
Parameters:
[in]bufThe input string
May not contain the \0 terminator.
[in]buflenThe input string size
[in]stripThe boolean value that indicates when this parser should do strip leading and whitespace trailing
This whitespace is still issued, but as type EINA_SIMPLE_XML_IGNORED.
[in]funcThe function to call back while parse does some action
The first parameter is the given user data, the second is the token type, the third is the pointer to the content's start (it's not a NULL terminated string), the fourth is where this content is located, that is buf (does not include tag start, for instance "<!DOCTYPE value>" the offset points at "value"), the fifth is the size of the content
Whenever this function returns EINA_FALSE the parser aborts.
[in]dataThe data to give as context to func
Returns:
EINA_TRUE on success, otherwise EINA_FALSE if it is aborted by the user or a parsing error occurs
const char* eina_simple_xml_tag_attributes_find ( const char *  buf,
unsigned  buflen 
)

Finds where the attributes start from given the contents of a tag are provided.

Since :
2.3.1
Remarks:
The tag contents are returned by eina_simple_xml_parse() when type is EINA_SIMPLE_XML_OPEN or EINA_SIMPLE_XML_OPEN_EMPTY.
Parameters:
[in]bufThe input string
May not contain \0 terminator.
[in]buflenThe input string size
Returns:
A pointer to the start of the attributes, it can be used to feed eina_simple_xml_attributes_parse()
NULL is returned if no attributes are found.