An open API service indexing awesome lists of open source software.

https://github.com/gechandesu/xmlencoder

An experimental XML encoder for V
https://github.com/gechandesu/xmlencoder

vlang vlang-package xml xml-encoder

Last synced: about 1 month ago
JSON representation

An experimental XML encoder for V

Awesome Lists containing this project

README

          

# XML encoder library for V

The `xmlencoder` module provides a way to marshal V structs into XML documents, similar to what the `json` module does.

Use `xml` attribute to set XML document and field names and encoder special options:

- a field with attribute `-` is omitted.
- a field with attribute `name,attr` becomes an attribute with the given name in the XML element.
- a field with attribute `,attr` becomes an attribute with the field name in the XML element.
- a field with attribute `,chardata` is written as character data, not as an XML element.
- a field with attribute `,cdata` is written as character data wrapped in one or more `` tags, not as an XML element.
- a field with attribute `,comment` is written as an XML comment. It must not contain the `--` string within it.

XML Namespaces are also supported. See examples below.

## Examples

```v
import xmlencoder

@[xml: 'user']
struct User {
first_name string @[xml: 'firstName']
last_name string @[xml: 'lastName']
age int
skipped int @[xml: '-']
}

fn main() {
user := User{'John', 'Doe', 27, -1}
assert xmlencoder.encode(user, pretty: true) == '

John
Doe
27
'.trim_indent()
}
```

Tag with attrubutes:

```v
@[xml: topology]
struct Topology {
sockets int @[xml: ',attr']
cores int @[xml: ',attr']
threads int @[xml: ',attr']
}

fn main() {
topo := Topology{1, 4, 1}
assert xmlencoder.encode(topo) == ""
}
```

Example with `,chardata`:

```v
@[xml: memory]
struct Memory {
unit string @[xml: ',attr']
value u64 @[xml: ',chardata']
}

fn main() {
mem := Memory{'MiB', 10240}
assert xmlencoder.encode(mem) == "10240"
}
```

For XML Namespaces specify `xmlns` attribute with comma separated `PREFIX,URI` arguments:

```v ignore
@[xmlns: 'exampl,https://example.com/xmlns/EXAMPL']
@[xml: 'metadata']
struct Metadata {
tag []string
}

fn main() {
m := Metadata{['foobar', 'foobaz']}
assert xmlencoder.encode(m, pretty: true) == "

foobar
foobaz
".trim_indent()
}
```