Reference · reference

xml module

2 min read · 2026-09-02 · config-weave 0.1.0

use xml parses and serializes XML over the Value type, for the configuration files that JSON and TOML do not cover, such as a .csproj or a web.config. config-weave registers it from wscript-std. Every function returns Result[…, string].

parse

rust
xml::parse(text: string) -> Result[Value, string]

Parses an XML document into a Value. Elements nest as maps keyed by element name. Attributes are collected under @attrs, text under #text, and repeated child elements with the same name become a list.

XMLValue
<cfg><name>weave</name></cfg>{"cfg": {"name": "weave"}}
<a k="v">text</a>{"a": {"@attrs": {"k": "v"}, "#text": "text"}}
<r><i>1</i><i>2</i></r>{"r": {"i": ["1", "2"]}}

Mixed content loses its order

Text interleaved with child elements is concatenated into one #text. <r>mixed<b>x</b>tail</r> parses to {"r": {"#text": "mixedtail", "b": "x"}}, so a round trip moves the text ahead of the child. This is fine for configuration files and wrong for documents. Produce documents with the template module instead.

to_string and to_string_pretty

rust
xml::to_string(value: Value) -> Result[string, string]
xml::to_string_pretty(value: Value) -> Result[string, string]

Serializes a Value in the mapping above back to XML, compact or indented.

rust
use fs
use xml

let doc = xml::parse(fs::read("/srv/app/web.config")?)?
let config = doc.get("configuration")