Synopsis#include <glib.h> enum GMarkupError; #define G_MARKUP_ERROR enum GMarkupParseFlags; GMarkupParseContext; GMarkupParser; gchar* g_markup_escape_text (const gchar *text, gssize length); gchar* g_markup_printf_escaped (const char *format, ...); gchar* g_markup_vprintf_escaped (const char *format, va_list args); gboolean g_markup_parse_context_end_parse (GMarkupParseContext *context, GError **error); void g_markup_parse_context_free (GMarkupParseContext *context); void g_markup_parse_context_get_position (GMarkupParseContext *context, gint *line_number, gint *char_number); const gchar* g_markup_parse_context_get_element (GMarkupParseContext *context); const GSList* g_markup_parse_context_get_element_stack (GMarkupParseContext *context); gpointer g_markup_parse_context_get_user_data (GMarkupParseContext *context); GMarkupParseContext* g_markup_parse_context_new (const GMarkupParser *parser, GMarkupParseFlags flags, gpointer user_data, GDestroyNotify user_data_dnotify); gboolean g_markup_parse_context_parse (GMarkupParseContext *context, const gchar *text, gssize text_len, GError **error); void g_markup_parse_context_push (GMarkupParseContext *context, GMarkupParser *parser, gpointer user_data); gpointer g_markup_parse_context_pop (GMarkupParseContext *context); enum GMarkupCollectType; gboolean g_markup_collect_attributes (const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, GError **error, GMarkupCollectType first_type, const gchar *first_attr, ...); DescriptionThe "GMarkup" parser is intended to parse a simple markup format that's a subset of XML. This is a small, efficient, easy-to-use parser. It should not be used if you expect to interoperate with other applications generating full-scale XML. However, it's very useful for application data files, config files, etc. where you know your application will be the only one writing the file. Full-scale XML parsers should be able to parse the subset used by GMarkup, so you can easily migrate to full-scale XML at a later time if the need arises. GMarkup is not guaranteed to signal an error on all invalid XML; the parser may accept documents that an XML parser would not. However, XML documents which are not well-formed[5] are not considered valid GMarkup documents. Simplifications to XML include:
The markup format does support:
Detailsenum GMarkupErrortypedef enum
{
G_MARKUP_ERROR_BAD_UTF8,
G_MARKUP_ERROR_EMPTY,
G_MARKUP_ERROR_PARSE,
/* The following are primarily intended for specific GMarkupParser
* implementations to set.
*/
G_MARKUP_ERROR_UNKNOWN_ELEMENT,
G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
G_MARKUP_ERROR_INVALID_CONTENT,
G_MARKUP_ERROR_MISSING_ATTRIBUTE
} GMarkupError;
Error codes returned by markup parsing.
G_MARKUP_ERROR#define G_MARKUP_ERROR Error domain for markup parsing. Errors in this domain will be from the GMarkupError enumeration. See GError for information on error domains. enum GMarkupParseFlagstypedef enum
{
G_MARKUP_DO_NOT_USE_THIS_UNSUPPORTED_FLAG = 1 << 0,
G_MARKUP_TREAT_CDATA_AS_TEXT = 1 << 1,
G_MARKUP_PREFIX_ERROR_POSITION = 1 << 2
} GMarkupParseFlags;
Flags that affect the behaviour of the parser.
GMarkupParseContexttypedef struct _GMarkupParseContext GMarkupParseContext;
A parse context is used to parse a stream of bytes that you expect to
contain marked-up text. See GMarkupParsertypedef struct {
/* Called for open tags <foo bar="baz"> */
void (*start_element) (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error);
/* Called for close tags </foo> */
void (*end_element) (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error);
/* Called for character data */
/* text is not nul-terminated */
void (*text) (GMarkupParseContext *context,
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error);
/* Called for strings that should be re-saved verbatim in this same
* position, but are not otherwise interpretable. At the moment
* this includes comments and processing instructions.
*/
/* text is not nul-terminated. */
void (*passthrough) (GMarkupParseContext *context,
const gchar *passthrough_text,
gsize text_len,
gpointer user_data,
GError **error);
/* Called on error, including one set by other
* methods in the vtable. The GError should not be freed.
*/
void (*error) (GMarkupParseContext *context,
GError *error,
gpointer user_data);
} GMarkupParser;
Any of the fields in GMarkupParser can be
g_markup_escape_text ()gchar* g_markup_escape_text (const gchar *text, gssize length); Escapes text so that the markup parser will parse it verbatim. Less than, greater than, ampersand, etc. are replaced with the corresponding entities. This function would typically be used when writing out a file to be parsed with the markup parser. Note that this function doesn't protect whitespace and line endings from being processed according to the XML rules for normalization of line endings and attribute values. Note also that if given a string containing them, this function will produce character references in the range of &x1; .. &x1f; for all control sequences except for tabstop, newline and carriage return. The character references in this range are not valid XML 1.0, but they are valid XML 1.1 and will be accepted by the GMarkup parser.
g_markup_printf_escaped ()gchar* g_markup_printf_escaped (const char *format, ...);
Formats arguments according to
const char *store = "Fortnum & Mason";
const char *item = "Tea";
char *output;
output = g_markup_printf_escaped ("<purchase>"
"<store>%s</store>"
"<item>%s</item>"
"</purchase>",
store, item);
Since 2.4 g_markup_vprintf_escaped ()gchar* g_markup_vprintf_escaped (const char *format, va_list args);
Formats the data in
Since 2.4 g_markup_parse_context_end_parse ()gboolean g_markup_parse_context_end_parse (GMarkupParseContext *context, GError **error);
Signals to the GMarkupParseContext that all data has been
fed into the parse context with
g_markup_parse_context_free ()void g_markup_parse_context_free (GMarkupParseContext *context); Frees a GMarkupParseContext. Can't be called from inside one of the GMarkupParser functions. Can't be called while a subparser is pushed.
g_markup_parse_context_get_position ()void g_markup_parse_context_get_position (GMarkupParseContext *context, gint *line_number, gint *char_number); Retrieves the current line number and the number of the character on that line. Intended for use in error messages; there are no strict semantics for what constitutes the "current" line number other than "the best number we could come up with for error messages."
g_markup_parse_context_get_element ()const gchar* g_markup_parse_context_get_element (GMarkupParseContext *context); Retrieves the name of the currently open element.
If called from the start_element or end_element handlers this will
give the element_name as passed to those functions. For the parent
elements, see
Since 2.2 g_markup_parse_context_get_element_stack ()const GSList* g_markup_parse_context_get_element_stack (GMarkupParseContext *context);
Retrieves the element stack from the internal state of the parser.
The returned GSList is a list of strings where the first item is
the currently open tag (as would be returned by
This function is intended to be used in the start_element and
end_element handlers where
Since 2.16 g_markup_parse_context_get_user_data ()gpointer g_markup_parse_context_get_user_data (GMarkupParseContext *context);
Returns the user_data associated with
Since 2.18 g_markup_parse_context_new ()GMarkupParseContext* g_markup_parse_context_new (const GMarkupParser *parser, GMarkupParseFlags flags, gpointer user_data, GDestroyNotify user_data_dnotify); Creates a new parse context. A parse context is used to parse marked-up documents. You can feed any number of documents into a context, as long as no errors occur; once an error occurs, the parse context can't continue to parse text (you have to free it and create a new parse context).
g_markup_parse_context_parse ()gboolean g_markup_parse_context_parse (GMarkupParseContext *context, const gchar *text, gssize text_len, GError **error); Feed some data to the GMarkupParseContext. The data need not be valid UTF-8; an error will be signaled if it's invalid. The data need not be an entire document; you can feed a document into the parser incrementally, via multiple calls to this function. Typically, as you receive data from a network connection or file, you feed each received chunk of data into this function, aborting the process if an error occurs. Once an error is reported, no further data may be fed to the GMarkupParseContext; all errors are fatal.
g_markup_parse_context_push ()void g_markup_parse_context_push (GMarkupParseContext *context, GMarkupParser *parser, gpointer user_data); Temporarily redirects markup data to a sub-parser.
This function may only be called from the start_element handler of
a GMarkupParser. It must be matched with a corresponding call to
All tags, text and other data between the matching tags is
redirected to the subparser given by
The end tag matching the start tag for which this call was made is
handled by the previous parser (which is given its own user_data)
which is why This function is not intended to be directly called by users interested in invoking subparsers. Instead, it is intended to be used by the subparsers themselves to implement a higher-level interface. As an example, see the following implementation of a simple parser that counts the number of tags encountered.
typedef struct
{
gint tag_count;
} CounterData;
static void
counter_start_element (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error)
{
CounterData *data = user_data;
data->tag_count++;
}
static void
counter_error (GMarkupParseContext *context,
GError *error,
gpointer user_data)
{
CounterData *data = user_data;
g_slice_free (CounterData, data);
}
static GMarkupParser counter_subparser =
{
counter_start_element,
NULL,
NULL,
NULL,
counter_error
};
In order to allow this parser to be easily used as a subparser, the following interface is provided:
void
start_counting (GMarkupParseContext *context)
{
CounterData *data = g_slice_new (CounterData);
data->tag_count = 0;
g_markup_parse_context_push (context, &counter_subparser, data);
}
gint
end_counting (GMarkupParseContext *context)
{
CounterData *data = g_markup_parse_context_pop (context);
int result;
result = data->tag_count;
g_slice_free (CounterData, data);
return result;
}
The subparser would then be used as follows:
static void start_element (context, element_name, ...)
{
if (strcmp (element_name, "count-these") == 0)
start_counting (context);
/* else, handle other tags... */
}
static void end_element (context, element_name, ...)
{
if (strcmp (element_name, "count-these") == 0)
g_print ("Counted %d tags\n", end_counting (context));
/* else, handle other tags... */
}
Since 2.18 g_markup_parse_context_pop ()gpointer g_markup_parse_context_pop (GMarkupParseContext *context); Completes the process of a temporary sub-parser redirection.
This function exists to collect the user_data allocated by a
matching call to This function is not intended to be directly called by users interested in invoking subparsers. Instead, it is intended to be used by the subparsers themselves to implement a higher-level interface.
Since 2.18 enum GMarkupCollectTypetypedef enum
{
G_MARKUP_COLLECT_INVALID,
G_MARKUP_COLLECT_STRING,
G_MARKUP_COLLECT_STRDUP,
G_MARKUP_COLLECT_BOOLEAN,
G_MARKUP_COLLECT_TRISTATE,
G_MARKUP_COLLECT_OPTIONAL = (1 << 16)
} GMarkupCollectType;
A mixed enumerated type and flags field. You must specify one type
(string, strdup, boolean, tristate). Additionally, you may
optionally bitwise OR the type with the flag
It is likely that this enum will be extended in the future to support other types.
g_markup_collect_attributes ()gboolean g_markup_collect_attributes (const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, GError **error, GMarkupCollectType first_type, const gchar *first_attr, ...); Collects the attributes of the element from the data passed to the GMarkupParser start_element function, dealing with common error conditions and supporting boolean values. This utility function is not required to write a parser but can save a lot of typing.
The
Following these arguments is a list of
"supported" attributes to collect. It is an
error to specify multiple attributes with the
same name. If any attribute not in the list
appears in the The GMarkupCollectType field allows specifying the type of collection to perform and if a given attribute must appear or is optional. The attribute name is simply the name of the attribute to collect.
The pointer should be of the appropriate type
(see the descriptions under
GMarkupCollectType) and may be
This function deals with issuing errors for missing attributes
(of type
Since 2.16 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||