Simple_XML
Eina » Tools

Simplistic relaxed SAX-like XML parser. More...

## Data Structures

struct | _Eina_Simple_XML_Attribute
struct | _Eina_Simple_XML_Node
struct | _Eina_Simple_XML_Node_Tag
struct | _Eina_Simple_XML_Node_Data

## Typedefs

typedef struct _Eina_Simple_XML_Node | Eina_Simple_XML_Node
typedef struct _Eina_Simple_XML_Node_Tag | Eina_Simple_XML_Node_Root
typedef struct _Eina_Simple_XML_Node_Tag | Eina_Simple_XML_Node_Tag
typedef struct _Eina_Simple_XML_Node_Data | Eina_Simple_XML_Node_Data
typedef struct _Eina_Simple_XML_Node_Data | Eina_Simple_XML_Node_CData
typedef struct _Eina_Simple_XML_Node_Data | Eina_Simple_XML_Node_Processing
typedef struct _Eina_Simple_XML_Node_Data | Eina_Simple_XML_Node_Doctype
typedef struct _Eina_Simple_XML_Node_Data | Eina_Simple_XML_Node_Doctype_Child
typedef struct _Eina_Simple_XML_Node_Data | Eina_Simple_XML_Node_Comment
typedef struct _Eina_Simple_XML_Attribute | Eina_Simple_XML_Attribute
typedef enum _Eina_Simple_XML_Node_Type | Eina_Simple_XML_Node_Type
typedef enum _Eina_Simple_XML_Type | Eina_Simple_XML_Type
typedef Eina_Bool(* | Eina_Simple_XML_Cb) (void *data, Eina_Simple_XML_Type type, const char *content, unsigned offset, unsigned length)
typedef Eina_Bool(* | Eina_Simple_XML_Attribute_Cb) (void *data, const char *key, const char *value)

## Enumerations

enum | _Eina_Simple_XML_Node_Type { EINA_SIMPLE_XML_NODE_ROOT = 0 , EINA_SIMPLE_XML_NODE_TAG , EINA_SIMPLE_XML_NODE_DATA , EINA_SIMPLE_XML_NODE_CDATA , EINA_SIMPLE_XML_NODE_PROCESSING , EINA_SIMPLE_XML_NODE_DOCTYPE , EINA_SIMPLE_XML_NODE_COMMENT , EINA_SIMPLE_XML_NODE_DOCTYPE_CHILD }
enum | _Eina_Simple_XML_Type { EINA_SIMPLE_XML_OPEN = 0 , EINA_SIMPLE_XML_OPEN_EMPTY , EINA_SIMPLE_XML_CLOSE , EINA_SIMPLE_XML_DATA , EINA_SIMPLE_XML_CDATA , EINA_SIMPLE_XML_ERROR , EINA_SIMPLE_XML_PROCESSING , EINA_SIMPLE_XML_DOCTYPE , EINA_SIMPLE_XML_COMMENT , EINA_SIMPLE_XML_IGNORED , EINA_SIMPLE_XML_DOCTYPE_CHILD }
| a simple XML type. More...

## Functions

EINA_API 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 XML string text.
EINA_API const char * | eina_simple_xml_tag_attributes_find (const char *buf, unsigned buflen)
| Given the contents of a tag, find where the attributes start.
EINA_API Eina_Bool | eina_simple_xml_attributes_parse (const char *buf, unsigned buflen, Eina_Simple_XML_Attribute_Cb func, const void *data)
| Given a buffer with xml attributes, parse them to key=value pairs.
EINA_API Eina_Bool | eina_simple_xml_attribute_w3c_parse (const char *buf, Eina_Simple_XML_Attribute_Cb func, const void *data)
| Given a buffer with the xml value of an attributes, parse them to key:value pairs.
EINA_API Eina_Simple_XML_Attribute * | eina_simple_xml_attribute_new (Eina_Simple_XML_Node_Tag *parent, const char *key, const char *value)
| Creates (and appends) new attribute to tag.
EINA_API void | eina_simple_xml_attribute_free (Eina_Simple_XML_Attribute *attr)
| Removes attribute from parent and deletes it.
EINA_API Eina_Simple_XML_Node_Tag * | eina_simple_xml_node_tag_new (Eina_Simple_XML_Node_Tag *parent, const char *name)
| Creates new tag.
EINA_API void | eina_simple_xml_node_tag_free (Eina_Simple_XML_Node_Tag *tag)
| Removes tag from parent and deletes it.
EINA_API 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.
EINA_API void | eina_simple_xml_node_data_free (Eina_Simple_XML_Node_Data *node)
| Removes data from parent and deletes it.
EINA_API 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.
EINA_API void | eina_simple_xml_node_cdata_free (Eina_Simple_XML_Node_Data *node)
| Removes cdata from parent and deletes it.
EINA_API 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 new doctype child.
EINA_API void | eina_simple_xml_node_doctype_child_free (Eina_Simple_XML_Node_Data *node)
| Removes doctype child from parent and deletes it.
EINA_API 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.
EINA_API void | eina_simple_xml_node_processing_free (Eina_Simple_XML_Node_Data *node)
| Removes processing from parent and deletes it.
EINA_API Eina_Simple_XML_Node_Doctype * | eina_simple_xml_node_doctype_new (Eina_Simple_XML_Node_Tag *parent, const char *contents, size_t length)
| Creates new doctype.
EINA_API void | eina_simple_xml_node_doctype_free (Eina_Simple_XML_Node_Data *node)
| Removes doctype from parent and deletes it.
EINA_API Eina_Simple_XML_Node_Comment * | eina_simple_xml_node_comment_new (Eina_Simple_XML_Node_Tag *parent, const char *contents, size_t length)
| Creates new comment.
EINA_API void | eina_simple_xml_node_comment_free (Eina_Simple_XML_Node_Data *node)
| Removes comment from parent and deletes it.
EINA_API Eina_Simple_XML_Node_Root * | eina_simple_xml_node_load (const char *buf, unsigned buflen, Eina_Bool strip)
| Loads a XML node tree based on the given string.
EINA_API void | eina_simple_xml_node_root_free (Eina_Simple_XML_Node_Root *root)
| Frees node tree build with eina_simple_xml_node_load().
EINA_API char * | eina_simple_xml_node_dump (Eina_Simple_XML_Node *node, const char *indent)
| Converts the node tree under the given element to a XML string.

## Detailed Description

Simplistic relaxed SAX-like XML parser.

This parser is far from being compliant with XML standard, but will do for most XMLs out there. If you know that your format is simple and will not vary in future with strange corner cases, then you can use it safely.

The parser is SAX like, that is, it will tokenize contents and call you back so you can take some action. No contents are allocated during this parser work and it's not recursive, so you can use it with a very large document without worries.

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

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

For more information, see this example.

## Typedef Documentation

## Eina_Simple_XML_Node_Doctype_Child

typedef struct _Eina_Simple_XML_Node_Data Eina_Simple_XML_Node_Doctype_Child

Since
  1.8

## Enumeration Type Documentation

## _Eina_Simple_XML_Node_Type

enum _Eina_Simple_XML_Node_Type

Enumerator

EINA_SIMPLE_XML_NODE_DOCTYPE_CHILD

Since
  1.8

## _Eina_Simple_XML_Type

enum _Eina_Simple_XML_Type

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 parser, like whitespace

EINA_SIMPLE_XML_DOCTYPE_CHILD

<!DOCTYPE_CHILD

Since
  1.8

## Function Documentation

## eina_simple_xml_parse()

EINA_API 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 XML string text.

Parameters

  [in] | buf | The input string. May not contain \0 terminator.
  [in] | buflen | The input string size.
  [in] | strip | Whenever this parser should strip leading and trailing whitespace. These whitespace will still be issued, but as type EINA_SIMPLE_XML_IGNORED.

  [in]

  func

  What to call back while parse to do some action. The first parameter is the given user data, the second is the token type, the third is the pointer to content start (it's not a NULL terminated string!), the fourth is where this content is located inside 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 return EINA_FALSE the parser will abort.

  [in] | data | What to give as context to func.

Returns
  EINA_TRUE on success, or EINA_FALSE if it was aborted by user or parsing error.

References EINA_FALSE, EINA_SIMPLE_XML_CDATA, EINA_SIMPLE_XML_CLOSE, EINA_SIMPLE_XML_COMMENT, EINA_SIMPLE_XML_DATA, EINA_SIMPLE_XML_DOCTYPE, EINA_SIMPLE_XML_DOCTYPE_CHILD, EINA_SIMPLE_XML_ERROR, EINA_SIMPLE_XML_IGNORED, EINA_SIMPLE_XML_OPEN, EINA_SIMPLE_XML_OPEN_EMPTY, EINA_SIMPLE_XML_PROCESSING, and EINA_TRUE.

Referenced by eina_simple_xml_node_load().

## eina_simple_xml_tag_attributes_find()

EINA_API const char * eina_simple_xml_tag_attributes_find | ( | const char * | buf,
|  | unsigned | buflen )

Given the contents of a tag, find where the attributes start.

Parameters

  [in] | buf | The input string. May not contain \0 terminator.
  [in] | buflen | The input string size.

Returns
  Pointer to the start of attributes, it can be used to feed eina_simple_xml_attributes_parse(). `NULL` is returned if no attributes were found.

The tag contents is returned by eina_simple_xml_parse() when type is EINA_SIMPLE_XML_OPEN or EINA_SIMPLE_XML_OPEN_EMPTY.

## eina_simple_xml_attributes_parse()

EINA_API Eina_Bool eina_simple_xml_attributes_parse | ( | const char * | buf,
|  | unsigned | buflen,
|  | Eina_Simple_XML_Attribute_Cb | func,
|  | const void * | data )

Given a buffer with xml attributes, parse them to key=value pairs.

Parameters

  [in] | buf | The input string. May not contain \0 terminator.
  [in] | buflen | The input string size.
  [in] | func | What to call back while parse to do 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 reference is just valid until the function return.
  [in] | data | Data to pass to the callback function.

Returns
  EINA_TRUE on success, or EINA_FALSE if it was aborted by user or parsing error.

References alloca(), EINA_FALSE, and EINA_TRUE.

## eina_simple_xml_attribute_w3c_parse()

EINA_API Eina_Bool eina_simple_xml_attribute_w3c_parse | ( | const char * | buf,
|  | Eina_Simple_XML_Attribute_Cb | func,
|  | const void * | data )

Given a buffer with the xml value of an attributes, parse them to key:value pairs.

Parameters

  [in] | buf | the input string. Need to contain \0 terminator.
  [in] | func | what to call back while parse to do 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 reference is just valid until the function return.
  [in] | data | data to pass to the callback function.

Returns
  EINA_TRUE on success or EINA_FALSE if it was aborted by user or parsing error.

Since
  1.14

References alloca(), EINA_FALSE, and EINA_TRUE.

## eina_simple_xml_attribute_new()

EINA_API Eina_Simple_XML_Attribute * eina_simple_xml_attribute_new | ( | Eina_Simple_XML_Node_Tag * | parent,
|  | const char * | key,
|  | const char * | value )

Creates (and appends) new attribute to tag.

Parameters

  [in,out] | parent | If provided, will be set in the resulting structure as well as the attribute will be appended to attributes list.
  [in] | key | Null-terminated string. Must not be `NULL`.
  [in] | value | Null-terminated string. If `NULL`, the empty string will be used.

Returns
  Newly allocated memory or `NULL` on error. This memory should be released with eina_simple_xml_attribute_free() or indirectly with eina_simple_xml_node_tag_free().

References eina_inlist_append(), EINA_INLIST_GET, EINA_MAGIC_SET, eina_stringshare_add(), and ERR.

## eina_simple_xml_attribute_free()

EINA_API void eina_simple_xml_attribute_free | ( | Eina_Simple_XML_Attribute * | attr | ) |

Removes attribute from parent and deletes it.

Parameters

  [in] | attr | attribute to release memory.

References EINA_INLIST_GET, eina_inlist_remove(), EINA_MAGIC_NONE, EINA_MAGIC_SET, and eina_stringshare_del().

## eina_simple_xml_node_tag_new()

EINA_API Eina_Simple_XML_Node_Tag * eina_simple_xml_node_tag_new | ( | Eina_Simple_XML_Node_Tag * | parent,
|  | const char * | name )

Creates new tag.

If parent is provided, it is automatically appended.

Parameters

  [in] | parent | If provided, will be set in the resulting structure as well as the tag will be appended to children list.
  [in] | name | Null-terminated string. Must not be `NULL`.

Returns
  Newly allocated memory or `NULL` on error. This memory should be released with eina_simple_xml_node_tag_free() or indirectly with eina_simple_xml_node_tag_free() of the parent.

References eina_inlist_append(), EINA_INLIST_GET, EINA_MAGIC_SET, eina_stringshare_add(), and ERR.

## eina_simple_xml_node_tag_free()

EINA_API void eina_simple_xml_node_tag_free | ( | Eina_Simple_XML_Node_Tag * | tag | ) |

Removes tag from parent and deletes it.

Parameters

  [in] | tag | to release memory.

References ERR.

## eina_simple_xml_node_data_new()

EINA_API 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.

Parameters

  [in,out] | parent | If provided, will be set in the resulting structure as well as the data will be appended to children list.
  [in] | contents | String to be used. Must not be `NULL`.
  [in] | length | Size in bytes of contents.

Returns
  Newly allocated memory or NULL on error. This memory should be released with eina_simple_xml_node_data_free() or indirectly with eina_simple_xml_node_tag_free() of the parent.

## eina_simple_xml_node_data_free()

EINA_API void eina_simple_xml_node_data_free | ( | Eina_Simple_XML_Node_Data * | node | ) |

Removes data from parent and deletes it.

Parameters

  [in] | node | to release memory.

References ERR.

## eina_simple_xml_node_cdata_new()

EINA_API 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.

Parameters

  [in,out] | parent | If provided, will be set in the resulting structure as well as the cdata will be appended to children list.
  [in] | contents | String to be used. Must not be `NULL`.
  [in] | length | Size in bytes of content.

Returns
  Newly allocated memory or `NULL` on error. This memory should be released with eina_simple_xml_node_cdata_free() or indirectly with eina_simple_xml_node_tag_free() of the parent.

## eina_simple_xml_node_cdata_free()

EINA_API void eina_simple_xml_node_cdata_free | ( | Eina_Simple_XML_Node_Data * | node | ) |

Removes cdata from parent and deletes it.

Parameters

  [in] | node | to release memory.

References ERR.

## eina_simple_xml_node_doctype_child_new()

EINA_API 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 new doctype child.

If parent is provided, it is automatically appended.

Parameters

  [in,out] | parent | If provided, will be set in the resulting structure as well as the doctype child will be appended to children list.
  [in] | contents | String to be used. Must not be `NULL`.
  [in] | length | size in bytes of content.

Returns
  Newly allocated memory or `NULL` on error. This memory should be released with eina_simple_xml_node_doctype_child_free() or indirectly with eina_simple_xml_node_tag_free() of the parent.

Since
  1.8

References EINA_SIMPLE_XML_NODE_DOCTYPE_CHILD.

## eina_simple_xml_node_doctype_child_free()

EINA_API void eina_simple_xml_node_doctype_child_free | ( | Eina_Simple_XML_Node_Data * | node | ) |

Removes doctype child from parent and deletes it.

Parameters

  [in] | node | to release memory.

Since
  1.8

References EINA_SIMPLE_XML_NODE_DOCTYPE_CHILD, and ERR.

## eina_simple_xml_node_processing_new()

EINA_API 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.

Parameters

  [in,out] | parent | If provided, will be set in the resulting structure as well as the processing will be appended to children list.
  [in] | contents | String to be used. Must not be `NULL`.
  [in] | length | Size in bytes of contents.

Returns
  Newly allocated memory or `NULL` on error. This memory should be released with eina_simple_xml_node_processing_free() or indirectly with eina_simple_xml_node_tag_free() of the parent.

## eina_simple_xml_node_processing_free()

EINA_API void eina_simple_xml_node_processing_free | ( | Eina_Simple_XML_Node_Data * | node | ) |

Removes processing from parent and deletes it.

Parameters

  [in] | node | processing to release memory.

References ERR.

## eina_simple_xml_node_doctype_new()

EINA_API Eina_Simple_XML_Node_Doctype * eina_simple_xml_node_doctype_new | ( | Eina_Simple_XML_Node_Tag * | parent,
|  | const char * | contents,
|  | size_t | length )

Creates new doctype.

If parent is provided, it is automatically appended.

Parameters

  [in,out] | parent | If provided, will be set in the resulting structure as well as the doctype will be appended to children list.
  [in] | contents | String to be used. Must not be `NULL`.
  [in] | length | Size in bytes of contents.

Returns
  Newly allocated memory or `NULL` on error. This memory should be released with eina_simple_xml_node_doctype_free() or indirectly with eina_simple_xml_node_tag_free() of the parent.

## eina_simple_xml_node_doctype_free()

EINA_API void eina_simple_xml_node_doctype_free | ( | Eina_Simple_XML_Node_Data * | node | ) |

Removes doctype from parent and deletes it.

Parameters

  [in] | node | doctype to release memory.

References ERR.

## eina_simple_xml_node_comment_new()

EINA_API Eina_Simple_XML_Node_Comment * eina_simple_xml_node_comment_new | ( | Eina_Simple_XML_Node_Tag * | parent,
|  | const char * | contents,
|  | size_t | length )

Creates new comment.

If parent is provided, it is automatically appended.

Parameters

  [in,out] | parent | If provided, will be set in the resulting structure as well as the comment will be appended to children list.
  [in] | contents | String to be used. Must not be `NULL`.
  [in] | length | Size in bytes of contents.

Returns
  Newly allocated memory or `NULL` on error. This memory should be released with eina_simple_xml_node_comment_free() or indirectly with eina_simple_xml_node_tag_free() of the parent.

## eina_simple_xml_node_comment_free()

EINA_API void eina_simple_xml_node_comment_free | ( | Eina_Simple_XML_Node_Data * | node | ) |

Removes comment from parent and deletes it.

Parameters

  [in] | node | comment to release memory.

References ERR.

## eina_simple_xml_node_load()

EINA_API Eina_Simple_XML_Node_Root * eina_simple_xml_node_load | ( | const char * | buf,
|  | unsigned | buflen,
|  | Eina_Bool | strip )

Loads a XML node tree based on the given string.

Parameters

  [in] | buf | The input string. May not contain \0 terminator.
  [in] | buflen | The input string size.
  [in] | strip | Whenever this parser should strip leading and trailing whitespace.

Returns
  Document root with children tags, or `NULL` on errors. Document with errors may return partial tree instead of `NULL`, we'll do our best to avoid returning nothing.

References EINA_MAGIC_SET, and eina_simple_xml_parse().

## eina_simple_xml_node_root_free()

EINA_API void eina_simple_xml_node_root_free | ( | Eina_Simple_XML_Node_Root * | root | ) |

Frees node tree build with eina_simple_xml_node_load().

Parameters

  [in] | root | Memory returned by eina_simple_xml_node_load()

References ERR.

## eina_simple_xml_node_dump()

EINA_API char * eina_simple_xml_node_dump | ( | Eina_Simple_XML_Node * | node,
|  | const char * | indent )

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

Parameters

  [in,out] | node | The base node to convert.
  [in] | indent | Indentation string, or `NULL` to disable it.

Returns
  `NULL` on errors, or a newly allocated string on success.

References eina_strbuf_free(), eina_strbuf_new(), and eina_strbuf_string_steal().