Module std::encoding::xml

XML parsing and manipulation.

Parse XML strings into node trees, inspect elements, extract attributes and text content, and serialize back to XML text.

Examples

import std::encoding::xml;

fn main() {
    let root = xml.parse("<book><title>Hew Guide</title></book>");
    let title = root.get_child(0);
    println(title.get_text());
    title.free();
    root.free();
}

Contents

Functions

Function parse

pub fn parse(s: string) -> Node

Parse an XML string into a Node tree.

Function to_string

pub fn to_string(node: Node) -> string

Serialize a Node tree back to an XML string.

Function get_attribute

pub fn get_attribute(node: Node, name: string) -> string

Get an attribute value from a node. Returns empty string if not found.

Function get_text

pub fn get_text(node: Node) -> string

Get the concatenated text content of a node and its descendants.

Types

Struct Node

An opaque XML node.

Represents either an element (with tag, attributes, and children) or a text node. Created by xml.parse(s) or by navigating into a parsed tree. Must be freed with free() when no longer needed.

Traits

Trait NodeMethods

Methods available on an XML Node.

Methods

fn to_string(self: Self) -> string

Serialize the node tree back to an XML string.

fn get_tag(self: Self) -> string

Get the tag name of an element node. Returns empty for text nodes.

fn get_attribute(self: Self, name: string) -> string

Get an attribute value by name. Returns empty string if not found.

fn children_count(self: Self) -> i64

Get the number of child nodes.

fn get_child(self: Self, index: i64) -> Node

Get a child node by index. Returns a new Node that must be freed.

fn get_text(self: Self) -> string

Get the concatenated text content of this node and its descendants.

fn is_element(self: Self) -> i32

Return 1 if this is an element node, 0 if text, -1 if null.

fn free(self: Self)

Release the node resources.