Add archive helpers and harden task runtime
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
:sig
|
||||
String xml_encode(DTree t)
|
||||
String xml_encode(DTree t, String root_name)
|
||||
|
||||
:params
|
||||
t : tree to serialize as XML
|
||||
root_name : optional element name to use when `t` is not already an element-shaped tree, defaults to `root`
|
||||
return value : XML string
|
||||
|
||||
:see
|
||||
>markup
|
||||
xml_decode
|
||||
json_encode
|
||||
0_DTree
|
||||
html_escape
|
||||
|
||||
:content
|
||||
Serializes a `DTree` into a simple XML string.
|
||||
|
||||
`xml_encode()` does not validate against a schema, DTD, or namespace rules. It is a structural converter for application data, similar in spirit to `json_encode()`.
|
||||
|
||||
Try the live example in the [XML demo](../demo/xml.uce).
|
||||
|
||||
The native element shape is:
|
||||
|
||||
```text
|
||||
node["name"] = "book"
|
||||
node["attrs"]["id"] = "b1"
|
||||
node["text"] = "optional text"
|
||||
node["children"] = list of child element nodes
|
||||
```
|
||||
|
||||
Example:
|
||||
|
||||
```uce
|
||||
DTree book;
|
||||
book["name"] = "book";
|
||||
book["attrs"]["id"] = "b1";
|
||||
|
||||
DTree title;
|
||||
title["name"] = "title";
|
||||
title["text"] = "UCE & XML";
|
||||
book["children"].push(title);
|
||||
|
||||
String xml = xml_encode(book);
|
||||
```
|
||||
|
||||
The result is:
|
||||
|
||||
```xml
|
||||
<book id="b1"><title>UCE & XML</title></book>
|
||||
```
|
||||
|
||||
For simple map/list/scalar trees, `xml_encode()` creates ordinary child elements:
|
||||
|
||||
```uce
|
||||
DTree payload;
|
||||
payload["title"] = "Hello";
|
||||
payload["count"] = "3";
|
||||
|
||||
String xml = xml_encode(payload, "payload");
|
||||
```
|
||||
|
||||
Result:
|
||||
|
||||
```xml
|
||||
<payload><count>3</count><title>Hello</title></payload>
|
||||
```
|
||||
|
||||
Notes:
|
||||
|
||||
- Element and attribute names are normalized to XML-name-safe strings when needed.
|
||||
- Text and attribute values are escaped.
|
||||
- List values use repeated `<item>` children.
|
||||
- Empty elements serialize as self-closing tags.
|
||||
- Map child order follows `DTree` map iteration order.
|
||||
Reference in New Issue
Block a user