After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 442929 - GMarkupParser documentation completion
GMarkupParser documentation completion
Status: RESOLVED OBSOLETE
Product: glib
Classification: Platform
Component: docs
unspecified
Other All
: Normal enhancement
: ---
Assigned To: gtkdev
gtkdev
: 536596 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2007-06-01 15:03 UTC by Verdin Hochzeit
Modified: 2018-05-24 11:02 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
add a simple parser example (7.08 KB, patch)
2012-11-23 18:54 UTC, David King
needs-work Details | Review

Description Verdin Hochzeit 2007-06-01 15:03:21 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
Comment 1 David King 2012-11-23 13:19:46 UTC
*** Bug 536596 has been marked as a duplicate of this bug. ***
Comment 2 David King 2012-11-23 18:54:20 UTC
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.
Comment 3 Philip Withnall 2017-10-26 09:33:27 UTC
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)
Comment 4 Luc Pi 2017-10-26 19:57:54 UTC
((( 
2017-->2007 GNOME bugzilla is a time machine
Always love it when it reminds me events from 10 years ago <3 
)))
Comment 5 GNOME Infrastructure Team 2018-05-24 11:02:31 UTC
-- 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.