Crate xmlserde

source ·
Expand description

xmlserde is another tool for serializing and deserializing XML. It is designed for easy and clear use.

Please add these dependencies in your Cargo.toml.

xmlserde = "0.6"
xmlserde_derives = "0.6"

§Deserialize

Suppose that XML struct is to be deserialized as below:

<person age="8">
    <name>Jeremy</name>
    <pet t="cat">Tom</pet>
    <pet t="dog">Spike</pet>
</person>

You can create a struct and derive the XmlDeserialize from xmlserde_derives, like:

use xmlserde_derives::XmlDeserialize;
#[derive(XmlDeserialize)]
pub struct Person {
    #[xmlserde(name = b"age", ty = "attr")]
    pub age: u16,
    #[xmlserde(name = b"pet", ty = "child")]
    pub pets: Vec<Pet>,
}

#[derive(XmlDeserialize)]
pub struct Pet {
    #[xmlserde(name = b"t", ty = "attr")]
    pub t: String,
    #[xmlserde(ty = "text")]
    pub name: String,
}

In xmlserde, you need to declare clearly that which tag and which type you are going to serde. Notice that it is a binary string for the name.

§Serialize

As for serializing, you need to derive the XmlSerialize.

§Enum

§For attribute value

Please check in xml_serde_enum section.

§For children element

You can define an enum like this.

#[derive(XmlSerialize, Deserialize)]
pub enum Pet{
    #[xmlserde(name = b"dog")]
    Dog(Dog),
    #[xmlserde(name = b"cat")]
    Cat(Cat),
}
pub struct Dog{}
pub struct Cat{}

In a field whose type is an enum, we can use ty = untag:

#[derive(XmlSerialize, Deserialize)]
pub struct Person {
    #[xmlserde(ty="untag")]
    pub pet: Pet,
}

In this case, Person can be serialized as

<person>
    <dog>
    ...
    </dog>
</person>

or

<person>
    <cat>
    ...
    </cat>
</person>

§Attributes

  • name: the tag of the XML element.
  • vec_size: creating a vector with the given capacity before deserilizing a element lists. vec_size=4 or if your initial capacity is defined in an attr, you can use like this vec_size="cnt".
  • default: assigning a parameter-free function to create a default value for a certain field. Notice that it requires the type of this value impls Eq and it will skip serializing when the value equals to the default one.
  • untag: see the Enum above.

§Examples

Please see LogiSheets for examples.

Re-exports§

Macros§

  • A macro to help you create mappings between string values in XML and Rust enums.

Structs§

  • Unparsed keeps the XML struct and will be serialized to XML with nothing change. It is helpful when you are debugging on deserializeing certain element.

Traits§

Functions§

  • The entry for deserializing. T should have declared the root by #[xmlserde(root=b"")] to tell the deserializer which tag is the start for deserializing.
  • The entry for deserializing. T should have declared the root by #[xmlserde(root=b"")] to tell the deserializer which tag is the start for deserializing.
  • The entry for serializing. T should have declared the root by #[xmlserde(root=b"")] to tell the serializer the tag name of the root.
  • The entry for serializing. T should have declared the root by #[xmlserde(root=b"")] to tell the serializer the tag name of the root. This function will add the header needed for a XML file.