GMarkupParser documentation completion
Submitted by Verdin Hochzeit
Link to original bug (#442929)
Description
Hi !
I tried to use the 'Simple XML Subset Parser' of Glib, but I didn't understand the Documentation. So i searched up google, and then, after ages, found some usable code. I think, it's not clear to most of the users, how it works. So I would recommend to give an example in the documentation.
That's my suggestion for an example code:
#include <glib.h>
#include <errno.h>
static void xml_start_element (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error);
static void xml_end_element (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error);
static void xml_text (GMarkupParseContext *context,
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error);
static void xml_passthrough (GMarkupParseContext *context,
const gchar *passthrough_text,
gsize text_len,
gpointer user_data,
GError **error);
static void xml_error (GMarkupParseContext *context,
GError *error,
gpointer user_data);
void
parse_xml_file (const char *fname)
{
GMarkupParser parser;
GMarkupParseContext *context;
gchar *buf;
gsize len;
context = NULL;
buf = NULL;
len = (gint)0;
/* Setup the Parser Callbacks */
parser.start_element = xml_start_element;
parser.end_element = xml_end_element;
parser.text = xml_text;
parser.passthrough = xml_passthrough;
parser.error = xml_error;
if (!g_file_get_contents (fname, &buf, &len, NULL))
{
g_warning("parser_parse (): g_file_get_contents (): \'%s\': %s\n",
fname, g_strerror (errno));
return;
}
context = g_markup_parse_context_new (&parser, 0, NULL, NULL);
g_markup_parse_context_parse (context, buf, len, NULL);
g_markup_parse_context_free (context);
g_free (buf);
}
static void
xml_start_element (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error)
{
g_print ("xml_start_element (): element_name = \'%s\'\n",
element_name);
}
static void
xml_end_element (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error)
{
g_print ("xml_end_element (): element_name = \'%s\'\n",
element_name);
}
static void
xml_text (GMarkupParseContext *context,
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error)
{
g_print ("xml_text (): text = \'%s\' (%u)\n",
text, (unsigned int)text_len);
}
static void
xml_passthrough (GMarkupParseContext *context,
const gchar *passthrough_text,
gsize text_len,
gpointer user_data,
GError **error)
{
g_print ("xml_passthrough (): passthrough_text = \'%s\' (%u)\n",
passthrough_text, (unsigned int)text_len);
}
static void
xml_error (GMarkupParseContext *context,
GError *error,
gpointer user_data)
{
g_print ("xml_error ()\n");
}
/* Example Code by Kolazomai */
It would also be possible not to read and parse a whole xml-file, but only a XML-char*. There's not too much to change. I wanted to add, that this/your Parser is great. ;-)
You may change this piece of sourcecode, if you like, but I would appreciate if my name ( 'Kolazomai' ) remained.
Thanks!
Best regards,
Kolazomai
Edited by Mathieu Duponchelle