format::xml +linux +x86_64

encoding::xml provides a simple parser of the useful subset of the XML 1.0 (Fifth Edition) specification as defined by the W3C. Features omitted are:

Attempting to parse an input file which does not conform to the supported subset of XML will return a syntax error. The purpose of this module is to support most XML files found in the wild, without supporting the lesser-used features that lead to problems like "billion laughs" vulnerabilities. If a fully conformant XML parser is required for your application, you will need to use a third-party XML implementation. Such an implementation should be able to shadow the standard library version and present a compatible API.



type attribute;
type elementend;
type elementstart;
type text;
type token;

// Undocumented types:
type parser;
type state;


type error;
type syntaxerr;


fn parse(*io::stream) (*parser | error);
fn parser_free(*parser) void;
fn scan(*parser) (token | void | error);
fn strerror(error) const str;


type attribute [link]

type attribute = (str, str);

An attribute of an XML element, e.g. foo="bar"

type elementend [link]

type elementend = str;

The end of an XML element, e.g. /> or </example>

type elementstart [link]

type elementstart = str;

The start of an XML element, e.g. <example

type text [link]

type text = str;

Text content of an XML element, e.g. baz or <![CDATA[baz]]>

type token [link]

type token = (elementstart | elementend | attribute | text);

Any valid XML token

type parser [link]

Show undocumented member
type parser = struct {
	orig: *io::stream,
	in: *io::stream,
	buf: [os::BUFSIZ]u8,
	state: state,
	tags: []str,
	namebuf: *io::stream,
	entbuf: *io::stream,
	textbuf: *io::stream,

type state [link]

Show undocumented member
type state = enum {


type error [link]

type error = (syntaxerr | utf8::invalid | io::error)!;

Any error which can occur during XML parsing.

type syntaxerr [link]

type syntaxerr = void!;

A syntax error was encountered in the document.


fn parse [link]

fn parse(in: *io::stream) (*parser | error);

Returns an XML parser which reads from a stream. The caller must call parser_free when they are finished with it.

****'s XML parser only supports UTF-8 encoded input files.

This function will attempt to read the XML prologue before returning, and will return an error if it is not valid.

fn parser_free [link]

fn parser_free(par: *parser) void;

Frees the resources associated with this parser. Does not close the underlying stream.

fn scan [link]

fn scan(par: *parser) (token | void | error);

Scans for and returns the next token. Tokens are borrowed from the parser and are not valid on subsequent calls to scan; use token_dup to extend their lifetime.

fn strerror [link]

fn strerror(err: error) const str;

Converts an error to a user-friendly string representation.