eina_simple_xml_parser_example_01_page

We are going to parse an XML sample file and print the data to stdout.

Like all examples we start by including Eina:

#include <Eina.h>
Eina Utility library.

We declare 2 booleans to keep track of tags:

Eina_Bool tag_login = EINA_FALSE;
#define EINA_FALSE
boolean value FALSE (numerical value 0)
Definition: eina_types.h:533
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:527
Eina_Bool tag_message = EINA_FALSE;

Here we declare some variables and initialize eina:

int
main(void)
{
FILE *file;
long size;
char *buffer;
Eina_Array *array;
EINA_API int eina_init(void)
Initializes the Eina library.
Definition: eina_main.c:291
Type for an array of data.
Definition: eina_array.h:229

We fill buffer with the XML data from chat.xml:

if ((file = fopen("chat.xml", "rb")))
{
fseek(file, 0, SEEK_END);
size = ftell(file);
fseek(file, 0, SEEK_SET);
if (size < 0)
{
EINA_LOG_ERR("Can't read chat.xml");
return 0;
}
if ((buffer = malloc(size)))
{
if (fread(buffer, 1, size, file) != (unsigned long)size)
#define EINA_LOG_ERR(fmt,...)
Logs a message with level ERROR on the default domain with the specified format.
Definition: eina_log.h:376

We will use an Eina_Array to store the data:

array = eina_array_new(10);
EINA_API Eina_Array * eina_array_new(unsigned int step)
Creates a new array.
Definition: eina_array.c:276

Here we call eina_simple_xml_parse(). We pass the buffer with data, its size, we ask to strip leading and trailing whitespace, we give the callback function and the array to store the formatted data:

_xml_tag_cb, array);
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.
Definition: eina_simple_xml_parser.c:284
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:539

This will loop over the array and print the data using _print callback:

eina_array_foreach(array, _print, NULL);
static Eina_Bool eina_array_foreach(Eina_Array *array, Eina_Each_Cb cb, void *fdata)
Iterates over an array using a callback function.

This is the main XML parser callback, it will check for known tags and get the corresponding values:

static Eina_Bool
_xml_tag_cb(void *data, Eina_Simple_XML_Type type, const char *content,
unsigned offset EINA_UNUSED, unsigned length)
{
char buffer[length+1];
Eina_Array *array = data;
char str[512] = {'\0'};
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339

We first check for opening tag:

if (type == EINA_SIMPLE_XML_OPEN)
@ EINA_SIMPLE_XML_OPEN
Definition: eina_simple_xml_parser.h:195

If we know the tag should have attributes, then we find them using eina_simple_xml_tag_attributes_find() and give them to another parsing function using eina_simple_xml_attributes_parse():

{
if(!strncmp("post", content, strlen("post")))
{
const char *tags = eina_simple_xml_tag_attributes_find(content,
length);
eina_simple_xml_attributes_parse(tags, length - (tags - content),
_xml_attr_cb, str);
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.
Definition: eina_simple_xml_parser.c:476
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.
Definition: eina_simple_xml_parser.c:501

We check for other known tags:

}
else if (!strncmp("login>", content, strlen("login>")))
{
tag_login = EINA_TRUE;
}
else if (!strncmp("message>", content, strlen("message>")))
{
tag_message = EINA_TRUE;

We then check data for corresponding tag:

}
}
else if (type == EINA_SIMPLE_XML_DATA)
{
if (tag_login == EINA_TRUE)
{
eina_strlcpy(buffer, content, sizeof(buffer));
eina_strlcat(str, "<", 1);
eina_strlcat(str, buffer, sizeof(str));
eina_strlcat(str, "> ", 2);
tag_login = EINA_FALSE;
@ EINA_SIMPLE_XML_DATA
Definition: eina_simple_xml_parser.h:198
EINA_API size_t eina_strlcat(char *dst, const char *src, size_t siz)
Appends a c-string.
Definition: eina_str.c:349
EINA_API size_t eina_strlcpy(char *dst, const char *src, size_t siz)
Copies a c-string to another.
Definition: eina_str.c:317

We are doing the formatting in same time and put all the <post> children in str.

}
else if (tag_message == EINA_TRUE)
{
eina_strlcpy(buffer, content, sizeof(buffer));
eina_strlcat(str, buffer, sizeof(str));
tag_message = EINA_FALSE;

Finally, we store our string in the array:

eina_array_push(array, strdup(str));
static Eina_Bool eina_array_push(Eina_Array *array, const void *data)
Appends a data item to an array.

This is the callback to parse the attributes, we check for key name and keep the value:

static Eina_Bool
_xml_attr_cb(void *data, const char *key, const char *value)
{
char *str = data;
if(!strcmp("id", key))
{
snprintf(str, sizeof(value) + 3, "(%s) ", value);

This is the function that simply print items of the array:

}
return EINA_TRUE;

You can see the full source code here.