<refentry id="glib-Simple-XML-Subset-Parser">
<refmeta>
<refentrytitle>Simple XML Subset Parser</refentrytitle>
<manvolnum>3</manvolnum>
<refmiscinfo>GLIB Library</refmiscinfo>
</refmeta>

<refnamediv>
<refname>Simple XML Subset Parser</refname><refpurpose>parses a subset of XML.</refpurpose>
</refnamediv>

<refsynopsisdiv><title>Synopsis</title>
<synopsis>

#include &lt;glib.h&gt;


enum        <link linkend="GMarkupError">GMarkupError</link>;
#define     <link linkend="G-MARKUP-ERROR-CAPS">G_MARKUP_ERROR</link>
enum        <link linkend="GMarkupParseFlags">GMarkupParseFlags</link>;
struct      <link linkend="GMarkupParseContext">GMarkupParseContext</link>;
struct      <link linkend="GMarkupParser">GMarkupParser</link>;
<link linkend="gchar">gchar</link>*      <link linkend="g-markup-escape-text">g_markup_escape_text</link>            (const <link linkend="gchar">gchar</link> *text,
                                             <link linkend="gssize">gssize</link> length);
<link linkend="gboolean">gboolean</link>    <link linkend="g-markup-parse-context-end-parse">g_markup_parse_context_end_parse</link>
                                            (<link linkend="GMarkupParseContext">GMarkupParseContext</link> *context,
                                             <link linkend="GError">GError</link> **error);
void        <link linkend="g-markup-parse-context-free">g_markup_parse_context_free</link>     (<link linkend="GMarkupParseContext">GMarkupParseContext</link> *context);
void        <link linkend="g-markup-parse-context-get-position">g_markup_parse_context_get_position</link>
                                            (<link linkend="GMarkupParseContext">GMarkupParseContext</link> *context,
                                             <link linkend="gint">gint</link> *line_number,
                                             <link linkend="gint">gint</link> *char_number);
<link linkend="GMarkupParseContext">GMarkupParseContext</link>* <link linkend="g-markup-parse-context-new">g_markup_parse_context_new</link>
                                            (const <link linkend="GMarkupParser">GMarkupParser</link> *parser,
                                             <link linkend="GMarkupParseFlags">GMarkupParseFlags</link> flags,
                                             <link linkend="gpointer">gpointer</link> user_data,
                                             <link linkend="GDestroyNotify">GDestroyNotify</link> user_data_dnotify);
<link linkend="gboolean">gboolean</link>    <link linkend="g-markup-parse-context-parse">g_markup_parse_context_parse</link>    (<link linkend="GMarkupParseContext">GMarkupParseContext</link> *context,
                                             const <link linkend="gchar">gchar</link> *text,
                                             <link linkend="gssize">gssize</link> text_len,
                                             <link linkend="GError">GError</link> **error);

</synopsis>
</refsynopsisdiv>





<refsect1>
<title>Description</title>
<para>
The "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.
</para>

<para>
GMarkup is not guaranteed to signal an error on all invalid XML; the
parser may accept documents that an XML parser would not. However, invalid XML 
documents are not considered valid GMarkup documents. 
</para>

<para>
Simplifications to XML include:
<itemizedlist>
<listitem>
<para>
Only UTF-8 encoding is allowed.
</para>
</listitem>
<listitem>
<para>
No user-defined entities.
</para>
</listitem>
<listitem>
<para>
Processing instructions, comments and the doctype declaration are "passed through" but are not 
interpreted in any way.
</para>
</listitem>
<listitem>
<para>
No DTD or validation.
</para>
</listitem>
</itemizedlist>
</para>

<para>
The markup format does support:
<itemizedlist>
<listitem>
<para>
Elements
</para>
</listitem>
<listitem>
<para>
Attributes
</para>
</listitem>
<listitem>
<para>
5 standard entities: <literal>&amp;amp; &amp;lt; &amp;gt; &amp;quot; &amp;apos;</literal>
</para>
</listitem>
<listitem>
<para>
Character references
</para>
</listitem>
<listitem>
<para>
Sections marked as CDATA
</para>
</listitem>
</itemizedlist>
</para>
</refsect1>

<refsect1>
<title>Details</title>
<refsect2>
<title><anchor id="GMarkupError">enum GMarkupError</title>
<programlisting>typedef enum
{
  G_MARKUP_ERROR_BAD_UTF8,
  G_MARKUP_ERROR_EMPTY,
  G_MARKUP_ERROR_PARSE,
  /* These three are primarily intended for specific GMarkupParser
   * implementations to set.
   */
  G_MARKUP_ERROR_UNKNOWN_ELEMENT,
  G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
  G_MARKUP_ERROR_INVALID_CONTENT
} GMarkupError;
</programlisting>
<para>
Error codes returned by markup parsing.
</para><informaltable pgwide="1" frame="none" role="enum">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row>
<entry><literal>G_MARKUP_ERROR_BAD_UTF8</literal></entry>
<entry>text being parsed was not valid UTF-8
</entry>
</row>
<row>
<entry><literal>G_MARKUP_ERROR_EMPTY</literal></entry>
<entry>document contained nothing, or only whitespace
</entry>
</row>
<row>
<entry><literal>G_MARKUP_ERROR_PARSE</literal></entry>
<entry>document was ill-formed
</entry>
</row>
<row>
<entry><literal>G_MARKUP_ERROR_UNKNOWN_ELEMENT</literal></entry>
<entry>error should be set by <link linkend="GMarkupParser">GMarkupParser</link> functions; element wasn't known
</entry>
</row>
<row>
<entry><literal>G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE</literal></entry>
<entry>error should be set by <link linkend="GMarkupParser">GMarkupParser</link> functions; attribute wasn't known
</entry>
</row>
<row>
<entry><literal>G_MARKUP_ERROR_INVALID_CONTENT</literal></entry>
<entry>error should be set by <link linkend="GMarkupParser">GMarkupParser</link> functions; something was wrong with contents of the document, e.g. invalid attribute value

</entry>
</row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="G-MARKUP-ERROR-CAPS">G_MARKUP_ERROR</title>
<programlisting>#define G_MARKUP_ERROR g_markup_error_quark ()
</programlisting>
<para>
Error domain for markup parsing. Errors in this domain will
be from the <link linkend="GMarkupError">GMarkupError</link> enumeration. See <link linkend="GError">GError</link> for information on 
error domains.
</para></refsect2>
<refsect2>
<title><anchor id="GMarkupParseFlags">enum GMarkupParseFlags</title>
<programlisting>typedef enum
{
  /* Hmm, can't think of any at the moment */
  G_MARKUP_DO_NOT_USE_THIS_UNSUPPORTED_FLAG = 1 &lt;&lt; 0
  
} GMarkupParseFlags;
</programlisting>
<para>
There are no flags right now. Pass "0" for the flags argument to all 
functions.
</para><informaltable pgwide="1" frame="none" role="enum">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row>
<entry><literal>G_MARKUP_DO_NOT_USE_THIS_UNSUPPORTED_FLAG</literal></entry>
<entry>flag you should not use.

</entry>
</row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="GMarkupParseContext">struct GMarkupParseContext</title>
<programlisting>struct GMarkupParseContext;</programlisting>
<para>
A parse context is used to parse a stream of bytes that you expect to
contain marked-up text. See <link linkend="g-markup-parse-context-new">g_markup_parse_context_new</link>(),
<link linkend="GMarkupParser">GMarkupParser</link>, and so on for more details.
</para></refsect2>
<refsect2>
<title><anchor id="GMarkupParser">struct GMarkupParser</title>
<programlisting>struct GMarkupParser
{
  /* Called for open tags &lt;foo bar="baz"&gt; */
  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 &lt;/foo&gt; */
  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);
};
</programlisting>
<para>
Any of the fields in <link linkend="GMarkupParser">GMarkupParser</link> can be <literal>NULL</literal>, in which case they
will be ignored. Except for the <parameter>error</parameter> function, any of these
callbacks can set an error; in particular the
<literal>G_MARKUP_ERROR_UNKNOWN_ELEMENT</literal>, <literal>G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE</literal>,
and <literal>G_MARKUP_ERROR_INVALID_CONTENT</literal> errors are intended to be set 
from these callbacks. If you set an error from a callback,
<link linkend="g-markup-parse-context-parse">g_markup_parse_context_parse</link>() will report that error back to its caller.
</para><informaltable pgwide="1" frame="none" role="struct">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row>
<entry>void (*<structfield>start_element</structfield>) (GMarkupParseContext *context,
                          const gchar         *element_name,
                          const gchar        **attribute_names,
                          const gchar        **attribute_values,
                          gpointer             user_data,
                          GError             **error)</entry>
<entry>Callback to invoke when the opening tag of an element
is seen.
</entry>
</row>
<row>
<entry>void (*<structfield>end_element</structfield>) (GMarkupParseContext *context,
                          const gchar         *element_name,
                          gpointer             user_data,
                          GError             **error)</entry>
<entry>Callback to invoke when the closing tag of an element is seen
</entry>
</row>
<row>
<entry>void (*<structfield>text</structfield>) (GMarkupParseContext *context,
                          const gchar         *text,
                          gsize                text_len,  
                          gpointer             user_data,
                          GError             **error)</entry>
<entry>Callback to invoke when some text is seen (text is always
inside an element)
</entry>
</row>
<row>
<entry>void (*<structfield>passthrough</structfield>) (GMarkupParseContext *context,
                          const gchar         *passthrough_text,
                          gsize                text_len,  
                          gpointer             user_data,
                          GError             **error)</entry>
<entry>Callback to invoke for comments, processing
instructions and doctype declarations; if you're re-writing the parsed document, write the
passthrough text back out in the same position
</entry>
</row>
<row>
<entry>void (*<structfield>error</structfield>) (GMarkupParseContext *context,
                          GError              *error,
                          gpointer             user_data)</entry>
<entry>Callback to invoke when an error occurs

</entry>
</row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="g-markup-escape-text">g_markup_escape_text ()</title>
<programlisting><link linkend="gchar">gchar</link>*      g_markup_escape_text            (const <link linkend="gchar">gchar</link> *text,
                                             <link linkend="gssize">gssize</link> length);</programlisting>
<para>
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.</para>
<para>

</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>text</parameter>&nbsp;:</entry>
<entry> some valid UTF-8 text
</entry></row>
<row><entry align="right"><parameter>length</parameter>&nbsp;:</entry>
<entry> length of <parameter>text</parameter> in bytes
</entry></row>
<row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry> escaped text
</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="g-markup-parse-context-end-parse">g_markup_parse_context_end_parse ()</title>
<programlisting><link linkend="gboolean">gboolean</link>    g_markup_parse_context_end_parse
                                            (<link linkend="GMarkupParseContext">GMarkupParseContext</link> *context,
                                             <link linkend="GError">GError</link> **error);</programlisting>
<para>
Signals to the <link linkend="GMarkupParseContext">GMarkupParseContext</link> that all data has been
fed into the parse context with <link linkend="g-markup-parse-context-parse">g_markup_parse_context_parse</link>().
This function reports an error if the document isn't complete,
for example if elements are still open.</para>
<para>

</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>context</parameter>&nbsp;:</entry>
<entry> a <link linkend="GMarkupParseContext">GMarkupParseContext</link>
</entry></row>
<row><entry align="right"><parameter>error</parameter>&nbsp;:</entry>
<entry> return location for a <link linkend="GError">GError</link>
</entry></row>
<row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry> <literal>TRUE</literal> on success, <literal>FALSE</literal> if an error was set
</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="g-markup-parse-context-free">g_markup_parse_context_free ()</title>
<programlisting>void        g_markup_parse_context_free     (<link linkend="GMarkupParseContext">GMarkupParseContext</link> *context);</programlisting>
<para>
Frees a <link linkend="GMarkupParseContext">GMarkupParseContext</link>. Can't be called from inside
one of the <link linkend="GMarkupParser">GMarkupParser</link> functions.</para>
<para>

</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>context</parameter>&nbsp;:</entry>
<entry> a <link linkend="GMarkupParseContext">GMarkupParseContext</link>
</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="g-markup-parse-context-get-position">g_markup_parse_context_get_position ()</title>
<programlisting>void        g_markup_parse_context_get_position
                                            (<link linkend="GMarkupParseContext">GMarkupParseContext</link> *context,
                                             <link linkend="gint">gint</link> *line_number,
                                             <link linkend="gint">gint</link> *char_number);</programlisting>
<para>
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."</para>
<para>

</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>context</parameter>&nbsp;:</entry>
<entry> a <link linkend="GMarkupParseContext">GMarkupParseContext</link>
</entry></row>
<row><entry align="right"><parameter>line_number</parameter>&nbsp;:</entry>
<entry> return location for a line number, or <literal>NULL</literal>
</entry></row>
<row><entry align="right"><parameter>char_number</parameter>&nbsp;:</entry>
<entry> return location for a char-on-line number, or <literal>NULL</literal>
</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="g-markup-parse-context-new">g_markup_parse_context_new ()</title>
<programlisting><link linkend="GMarkupParseContext">GMarkupParseContext</link>* g_markup_parse_context_new
                                            (const <link linkend="GMarkupParser">GMarkupParser</link> *parser,
                                             <link linkend="GMarkupParseFlags">GMarkupParseFlags</link> flags,
                                             <link linkend="gpointer">gpointer</link> user_data,
                                             <link linkend="GDestroyNotify">GDestroyNotify</link> user_data_dnotify);</programlisting>
<para>
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).</para>
<para>

</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>parser</parameter>&nbsp;:</entry>
<entry> a <link linkend="GMarkupParser">GMarkupParser</link>
</entry></row>
<row><entry align="right"><parameter>flags</parameter>&nbsp;:</entry>
<entry> one or more <link linkend="GMarkupParseFlags">GMarkupParseFlags</link>
</entry></row>
<row><entry align="right"><parameter>user_data</parameter>&nbsp;:</entry>
<entry> user data to pass to <link linkend="GMarkupParser">GMarkupParser</link> functions
</entry></row>
<row><entry align="right"><parameter>user_data_dnotify</parameter>&nbsp;:</entry>
<entry> user data destroy notifier called when the parse context is freed
</entry></row>
<row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry> a new <link linkend="GMarkupParseContext">GMarkupParseContext</link>
</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="g-markup-parse-context-parse">g_markup_parse_context_parse ()</title>
<programlisting><link linkend="gboolean">gboolean</link>    g_markup_parse_context_parse    (<link linkend="GMarkupParseContext">GMarkupParseContext</link> *context,
                                             const <link linkend="gchar">gchar</link> *text,
                                             <link linkend="gssize">gssize</link> text_len,
                                             <link linkend="GError">GError</link> **error);</programlisting>
<para>
Feed some data to the <link linkend="GMarkupParseContext">GMarkupParseContext</link>. 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 <link linkend="GMarkupParseContext">GMarkupParseContext</link>; all errors are fatal.</para>
<para>

</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>context</parameter>&nbsp;:</entry>
<entry> a <link linkend="GMarkupParseContext">GMarkupParseContext</link>
</entry></row>
<row><entry align="right"><parameter>text</parameter>&nbsp;:</entry>
<entry> chunk of text to parse
</entry></row>
<row><entry align="right"><parameter>text_len</parameter>&nbsp;:</entry>
<entry> length of <parameter>text</parameter> in bytes
</entry></row>
<row><entry align="right"><parameter>error</parameter>&nbsp;:</entry>
<entry> return location for a <link linkend="GError">GError</link>
</entry></row>
<row><entry align="right"><emphasis>Returns</emphasis> :</entry><entry> <literal>FALSE</literal> if an error occurred, <literal>TRUE</literal> on success
</entry></row>
</tbody></tgroup></informaltable></refsect2>

</refsect1>




</refentry>
