5 unstable releases

0.3.0 Apr 20, 2021
0.2.3 Jul 1, 2019
0.2.1 Mar 21, 2019
0.2.0 Mar 21, 2019
0.1.0 Mar 16, 2019

#741 in Encoding

Download history 448/week @ 2024-06-17 418/week @ 2024-06-24 202/week @ 2024-07-01 230/week @ 2024-07-08 395/week @ 2024-07-15 371/week @ 2024-07-22 464/week @ 2024-07-29 411/week @ 2024-08-05 285/week @ 2024-08-12 265/week @ 2024-08-19 213/week @ 2024-08-26 360/week @ 2024-09-02 353/week @ 2024-09-09 284/week @ 2024-09-16 311/week @ 2024-09-23 454/week @ 2024-09-30

1,435 downloads per month
Used in 3 crates

MIT license

38KB
620 lines

Simple XML Serialization

This is a Rust crate for serialization of data to XML. XMLElements can either be built manually, or the simple_xml_serialize_macro crate can be used to generate From implementations for structs.

Example Usage

use simple_xml_serialize::XMLElement;

fn main() {
    // build up your XMLElement with individual calls ...
    let mut ele = XMLElement::new("person");
    ele.add_attr("age", 28); // accept any value that implements `ToString`.
    ele.set_text("John Doe");

    // ... or with the builder pattern
    let sub_ele = XMLElement::new("person")
        .attr("age", 4)
        .text("Jane Doe");

    ele.add_element(sub_ele); // `add_element` accepts values that implement `Into<XMLElement>`

    let expected = r#"<person age="28"><person age="4">Jane Doe</person>John Doe</person>"#;
    assert_eq!(expected, ele.to_string());
    println!("{}",  ele.to_string_pretty("\n", "\t")); // specify your preferred newline and indentation for pretty printing

    ele.set_text("John Doe > John Deere"); // illegal characters in text will be substituted e.g. > becomes &gt;
    let expected = r#"<person age="28"><person age="4">Jane Doe</person>John Doe &gt; John Deere</person>"#;
    assert_eq!(expected, ele.to_string());

   
    ele.set_text("<![CDATA[John Doe > John Deere]]>"); // illegal characters in CDATA tags are respected
    let expected = r#"<person age="28"><person age="4">Jane Doe</person><![CDATA[John Doe > John Deere]]></person>"#;
    assert_eq!(expected, ele.to_string());
}

Using simple_xml_serialize_macro

Using this proc_macro crate allows annotating structs with #[xml_element("...")] to generate From implementations of your struct to XMLElement. Individual fields are annotated with sxs_type_attr, sxs_type_text, sxs_type_element, and sxs_type_multi_element. Any fields not annotated are ignored.

use simple_xml_serialize::XMLElement;
use simple_xml_serialize_macro::xml_element;

#[xml_element("custom_name_here")]
struct MyPoint {
    // default for attrs is the name of the field
    #[sxs_type_attr] 
    lon: f32,

    // attrs can be renamed
    #[sxs_type_attr(rename="lat")] 
    latitude: f32,

    #[sxs_type_attr]
    active: bool,

    // nested XMLElements and collections of XMLElements can be renamed
    #[sxs_type_element] 
    name: MyName,
    #[sxs_type_multi_element(rename="id")] 
    names: Vec<MyName>
}

#[xml_element("Identifier")]
struct MyName {
    #[sxs_type_text]
    val: String,
}

fn main() {
    let my_point = MyPoint {
        latitude: 43.38,
        lon: 60.11,
        active: true,
        name: MyName{val: "p0".to_string()},
        names: vec![MyName{val: "p1".to_string()},MyName{val: "p2".to_string()}]
    };
    
    let my_point_xml = XMLElement::from(my_point); // can also take refs `&my_point`
    let expected = r#"<custom_name_here lon="60.11" lat="43.38" active="true"><Identifier>p0</Identifier><id>p1</id><id>p2</id></custom_name_here>"#;
    assert_eq!(expected, my_point_xml.to_string());

    let expected = r#"<custom_name_here lon="60.11" lat="43.38" active="true">
  <Identifier>
    p0
  </Identifier>
  <id>
    p1
  </id>
  <id>
    p2
  </id>
</custom_name_here>"#;
    assert_eq!(expected, my_point_xml.to_string_pretty("\n", "  ")); 
}

No runtime deps