GNOME Bugzilla – Bug 442929
GMarkupParser documentation completion
Last modified: 2018-05-24 11:02:31 UTC
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: <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 */ </code> 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
*** Bug 536596 has been marked as a duplicate of this bug. ***
Created attachment 229741 [details] [review] add a simple parser example The attachment adds a GMarkup parser example, based on Luc Pionchon's example in bug 536596. I can add some explanatory text to go alongside the example, if desired.
Review of attachment 229741 [details] [review]: Sorry for the short delay in reviewing this patch. Needs a bit of updating, but looks generally useful. :-) ::: glib/gmarkup.c @@ +83,3 @@ * </itemizedlist> + * + * <example id="gmarkup-example"><title>Example GMarkup parser</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../glib/tests/markup-example.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example> This will need porting to Markdown. IIRC the common practice for examples now is to hyperlink to them. Grep for `example-cmdline3` in gapplicationcommandline.c for an exemplary example of linking to an example, for example. ::: glib/tests/Makefile.am @@ +64,3 @@ +TEST_PROGS += markup-example +markup_example_LDADD = $(progs_ldadd) If this is going to be run as a test, it should support TAP output. I suggest it’s not run as a test, and is just built (and not installed) as an example program instead. Its sources should definitely be distributed in the tarball though. meson.build will need to be modified as well. ::: glib/tests/markup-example.c @@ +1,1 @@ +#include <glib.h> Needs a license header. @@ +117,3 @@ +foo_parser_error (GMarkupParseContext *context, + GError *error, + gpointer user_data) Nitpick: Weird spacing between function name and `(`. Drop it? @@ +122,3 @@ +} + +int main () int main (void)
((( 2017-->2007 GNOME bugzilla is a time machine Always love it when it reminds me events from 10 years ago <3 )))
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/glib/issues/95.