77 lines
1.6 KiB
Plaintext
77 lines
1.6 KiB
Plaintext
:sig
|
|
String xml_encode(DValue t)
|
|
String xml_encode(DValue 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_DValue
|
|
html_escape
|
|
|
|
:content
|
|
Serializes a `DValue` 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
|
|
DValue book;
|
|
book["name"] = "book";
|
|
book["attrs"]["id"] = "b1";
|
|
|
|
DValue 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
|
|
DValue 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 `DValue` map iteration order.
|