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 = match xml.parse("<book><title>Hew Guide</title></book>") {
        .Ok(node) => node,
        .Err(reason) => panic(reason),
    };
    let title = root.get_child(0);
    println(title.get_text());
    title.close();
    root.close();
}

Contents

Functions

Function parse

pub fn parse(s: string) -> Result<Node, string>

Parse an XML string without panicking.

Returns the parsed Node on success or the parser's error message when s is not well-formed XML.

Function is_wellformed

pub fn is_wellformed(s: string) -> bool

Report whether s is well-formed XML, without constructing a Node.

Prefer parse(s) when the parsed node or error message is needed.

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

Fields

handle: NodeHandle

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.