1 xtk_xml
liquibyte edited this page 2026-11-19 16:23:20 -06:00

xtk_xml.h

Reads XML or HTML text into a tree of elements and text.

This module parses XML or HTML text into a tree of XtkXmlNode nodes: elements with their attributes, text, and comments. It knows no schema: what the elements mean is up to the caller, which walks the tree. It can also turn a part of the tree back into plain text or into Markdown. It does not draw anything and needs no X display. See also xtk_csv.h and xtk_json.h.

What it reads.

  • Nested elements, attributes in single or double quotes or (as HTML allows) without quotes, and self-closing tags (<br/>).
  • Text, with the entities &amp;, &lt;, &gt;, &quot;, &apos;, &#NNN; and &#xHH; decoded, and those the DOCTYPE declares (below). Other named entities (such as &nbsp;) stay as written.
  • CDATA sections, whose content is kept as it is, without decoding.
  • Comments, which become nodes with the tag "#comment".
  • Processing instructions (<?xml ...?>) and <!DOCTYPE ...>, which are skipped. The general entities a DOCTYPE's internal subset declares (<!ENTITY fill_color "#FFFFFF">) are decoded in the attributes and text that follow, as some SVG files need. An entity is expanded at most 4 deep, and not past 64 KB, so a document cannot make itself huge. Parameter and external entities are not read.
  • A UTF-8 byte order mark at the start, which is skipped.

The tree. xtk_xml_parse() and xtk_xml_parse_html() return a root node with the tag ""; the elements at the top of the document are its children. There are three kinds of node:

  • An element has tag set to its name and text NULL. Its content is in its children.
  • A text node has tag NULL and text set to the decoded text. Text inside an element is kept as it is, so the white space between elements gives text nodes too.
  • A comment has tag equal to "#comment" and text set to the comment body, not decoded.

Each node owns its attributes and children, so xtk_xml_node_destroy() on the root frees the whole tree.

XML or HTML. Use xtk_xml_parse() for XML, where an empty element must be written <br/>. Use xtk_xml_parse_html() for HTML, where the void elements br, img, hr, meta, link, input, area, base, col, embed, param, source, track and wbr (in any case) are closed without a slash. Tag names keep the case of the text; use xtk_xml_node_find_child_case_insensitive() to find HTML tags.

Example.

#include <stdio.h>
#include <stdlib.h>
#include "xtk_xml.h"

XtkXmlNode *root = xtk_xml_parse("<note id=\"7\"><title>Shopping</title>"
                                 "<body>Milk &amp; <b>bread</b></body></note>");
XtkXmlNode *note = xtk_xml_node_find_child(root, "note");
char title[256];
xtk_xml_node_get_text(xtk_xml_node_find_child(note, "title"), title, sizeof(title));
char *body = xtk_xml_node_extract_markdown(xtk_xml_node_find_child(note, "body"));
printf("%s (%s): %s\n", title, xtk_xml_node_get_attribute(note, "id"), body);
free(body);
xtk_xml_node_destroy(root);
 * 

Limitations.

  • It is not a validating parser and has no namespace or external entity support, and of a DTD it reads only the general entities. On bad input it returns the part of the tree it could read.
  • A closing tag is matched by name (case ignored in HTML). The closing tag of an element further out also closes the elements inside it that were left open; a closing tag that matches no open element is skipped. An element that is never closed takes the rest of the text.
  • An unquoted attribute value runs to the next blank or the end of the tag. An attribute with no value gets the value "".
  • Every comment becomes a node, wherever it is.
  • Text outside every element is dropped.
  • Elements nested more than 500 deep are not nested further: each deeper start tag gives an empty node, and what it holds is read at that depth.
  • Allocation failures are not checked.
#include "xtk_xml.h"

Types: XtkXmlAttribute, XtkXmlNode

Functions: xtk_xml_parse, xtk_xml_parse_html, xtk_xml_node_destroy, xtk_xml_node_get_attribute, xtk_xml_node_find_child, xtk_xml_node_find_child_case_insensitive, xtk_xml_node_get_text, xtk_xml_node_extract_text, xtk_xml_node_extract_text_excluding, xtk_xml_node_extract_markdown, xtk_xml_node_extract_markdown_excluding

Types

XtkXmlAttribute

One attribute of an element.

Field Type Description
name char * The attribute's name, as written.
value char * The attribute's value, with entities decoded. "" when the attribute has no value.

XtkXmlNode

One node of the tree: an element, a text node or a comment, with its children.

Field Type Description
tag char * The element's name; "#comment" for a comment; NULL for a text node; "" for the root.
text char * For a text node: the decoded text (CDATA content is not decoded). For a comment: the comment body, not decoded. NULL for an element.
attributes XtkXmlAttribute * The element's attributes, in the order of the text.
attribute_count int The number of attributes.
children struct XtkXmlNode ** The child nodes, in the order of the text.
child_count int The number of children.

Functions

xtk_xml_parse

XtkXmlNode *xtk_xml_parse(const char *text);

Parses XML text into a tree.

An empty element must be written with a slash (<br/>). A leading UTF-8 byte order mark is skipped.

Parameter Description
text NUL-terminated XML text, or NULL.

Returns: A new root node with the tag "", whose children are the top-level elements and comments of the document. It is never NULL, even for NULL, empty or bad input. The caller frees it with xtk_xml_node_destroy().

xtk_xml_parse_html

XtkXmlNode *xtk_xml_parse_html(const char *text);

Parses HTML text into a tree.

The same as xtk_xml_parse(), but the HTML void elements (br, img, hr and the others listed at the top of this page) are closed even when they are written without a slash, as in text<BR>more text.

Parameter Description
text NUL-terminated HTML text, or NULL.

Returns: A new root node, never NULL, as for xtk_xml_parse(). The caller frees it with xtk_xml_node_destroy().

xtk_xml_node_destroy

void xtk_xml_node_destroy(XtkXmlNode *node);

Frees a node with all of its attributes and children.

Parameter Description
node The node to free, usually the root. NULL is allowed and does nothing.

xtk_xml_node_get_attribute

const char *xtk_xml_node_get_attribute(const XtkXmlNode *node, const char *name);

Finds the value of an attribute by its name.

The name is compared with case, as in XML.

Parameter Description
node The element, or NULL.
name The attribute name. It must not be NULL.

Returns: The value of the first attribute with that name, or NULL when node is NULL or has no such attribute.

xtk_xml_node_find_child

XtkXmlNode *xtk_xml_node_find_child(const XtkXmlNode *node, const char *tag);

Finds the first child element with a given tag.

Only the direct children are searched, and the tag is compared with case. Text nodes never match.

Parameter Description
node The parent node, or NULL.
tag The tag name. It must not be NULL.

Returns: The first matching child, or NULL when node is NULL or has no such child.

xtk_xml_node_find_child_case_insensitive

XtkXmlNode *xtk_xml_node_find_child_case_insensitive(
    const XtkXmlNode *node,
    const char *tag);

Finds the first child element with a given tag, in any case.

The same as xtk_xml_node_find_child(), but <DL> and <dl> match the same tag. Use it for HTML, whose tag names do not depend on case.

Parameter Description
node The parent node, or NULL.
tag The tag name, in any case. It must not be NULL.

Returns: The first matching child, or NULL when node is NULL or has no such child.

xtk_xml_node_get_text

void xtk_xml_node_get_text(const XtkXmlNode *node, char *out, unsigned long out_size);

Copies the text of a node's direct text children into a buffer.

The text of each direct text-node child is joined in order. The text of child elements is not included; use xtk_xml_node_extract_text() for that. This suits an element whose content is plain text, such as <title>Shopping</title>.

Parameter Description
node The node, or NULL (which gives "").
out The buffer that receives the text. It is always NUL-terminated, and the text is cut when it does not fit.
out_size The size of out in bytes, the NUL included. When it is 0, or out is NULL, nothing is written.

xtk_xml_node_extract_text

char *xtk_xml_node_extract_text(const XtkXmlNode *node);

Gives all the text under a node, at any depth, as plain text.

The text nodes under node are joined in the order of the document. Comments are left out. A line break is added after each div, p, br, li and tr element (in any case), so paragraphs stay on their own lines; other elements add nothing, so written in <b>bold</b> text gives one line.

Parameter Description
node The node, or NULL (which gives "").

Returns: A new NUL-terminated string, which the caller frees with free().

xtk_xml_node_extract_text_excluding

char *xtk_xml_node_extract_text_excluding(
    const XtkXmlNode *node,
    const char *exclude_tag_case_insensitive);

Gives the text under a node as plain text, without some elements.

The same as xtk_xml_node_extract_text(), but elements whose tag is exclude_tag_case_insensitive (in any case) are skipped with everything inside them. For example, an HTML outline entry <DD> holds its own text and a nested <DL> with the entries below it; excluding "dl" gives the entry's own text only.

Parameter Description
node The node, or NULL (which gives "").
exclude_tag_case_insensitive The tag of the elements to skip, in any case, or NULL to skip none.

Returns: A new NUL-terminated string, which the caller frees with free().

xtk_xml_node_extract_markdown

char *xtk_xml_node_extract_markdown(const XtkXmlNode *node);

Gives the text under a node as Markdown, keeping its formatting.

Like xtk_xml_node_extract_text(), but the formatting elements become Markdown. Both common HTML tags and the tags of Tomboy-style note files are known (tags in any case):

  • b, strong, bold: **text**
  • i, em, italic: *text*
  • u, underline: _text_
  • s, strike, del, strikethrough: ~~text~~
  • tt, code, or a style attribute that names monospace: the text in backticks
  • mark, highlight: ==text==
  • sup: ^text^; sub: ~text~
  • a with href: [text](href)
  • a #rrggbb color in style="color:..." or <font color="...">: <span style="color:#rrggbb">text</span>; and the same for background-color
  • size:huge, size:large: **text**; other size: tags: plain text
  • link:url: [url](url); other link: tags: plain text
  • list-item: a line - text; list: its items
  • en-todo with checked="true": - [x]; otherwise - [ ]

Line breaks are added after div, p, br, li and tr elements, as in xtk_xml_node_extract_text(); they are single \n characters, which Markdown joins into one paragraph unless the caller turns them into hard breaks. Comments are left out.

In the text itself, each backslash, backtick, *, _, ~, ^, =, [, < and # gets a backslash in front, so it cannot be read as Markdown syntax. Inside a code span the text is kept as it is, since Markdown would show the backslash there.

Parameter Description
node The node, or NULL (which gives "").

Returns: A new NUL-terminated Markdown string, which the caller frees with free().

xtk_xml_node_extract_markdown_excluding

char *xtk_xml_node_extract_markdown_excluding(
    const XtkXmlNode *node,
    const char *exclude_tag_case_insensitive);

Gives the text under a node as Markdown, without some elements.

The same as xtk_xml_node_extract_markdown(), but elements whose tag is exclude_tag_case_insensitive (in any case) are skipped with everything inside them, as in xtk_xml_node_extract_text_excluding().

Parameter Description
node The node, or NULL (which gives "").
exclude_tag_case_insensitive The tag of the elements to skip, in any case, or NULL to skip none.

Returns: A new NUL-terminated Markdown string, which the caller frees with free().