I'm trying to parse this kind of XML with serde and an xml crate, but I'm not sure it's possible with serde:
<vm>
<id>15</id>
<template>
<tag1>content</tag1>
<tag2>content</tag2>
<vec1>
<tag3>content</tag3>
<tag4>content</tag4>
</vec1>
<tag2>content</tag2>
<vec1>
<tag3>content</tag3>
</vec1>
<vec2>
<tag3>content</tag3>
</vec2>
</template>
</vm>
All tag names tagX
and vecX
keys are dynamic (and not necessarily unique), all others names are static and known.
The content inside template
have only two possible forms:
Either a representation of a (key, value) "pair": <key>value</key>
Or a a representation of a "vector" (vec_key, collection of pairs): <vec_key><key>value</key><key2>value2</key2> ... </vec_key>
I'm trying to represent the data to something close to this (with tag name in first String):
enum Element {
Pair(String, String),
Vector(String, Vec<(String, String)>),
}
pub struct VM {
id: i64,
template: Vec<Element>,
}
So the above XML would be deserialized to something like:
[
Pair("tag1", "content"),
Pair("tag2", "content"),
Vector("vec1", [("tag3", "content"),("tag4", "content")]),
Pair("tag2", "content"),
Vector("vec1", [("tag3", "content")]),
Vector("vec2", [("tag3", "content")])
]
I'm open to modify a bit the representation but I just don't want to store the datas in complex nested data structures.
Is it possible with Serde ?
For the context I did the same with Golang and encoding/xml module, and I was able to mix regular structure deserialization with custom deserialization (working directly with the pull parser for the custom part)