5个不稳定版本

0.3.0 2021年4月20日
0.2.3 2019年7月1日
0.2.1 2019年3月21日
0.2.0 2019年3月21日
0.1.0 2019年3月16日

#677 in 编码

Download history 569/week @ 2024-03-16 460/week @ 2024-03-23 377/week @ 2024-03-30 285/week @ 2024-04-06 319/week @ 2024-04-13 375/week @ 2024-04-20 262/week @ 2024-04-27 254/week @ 2024-05-04 408/week @ 2024-05-11 379/week @ 2024-05-18 344/week @ 2024-05-25 452/week @ 2024-06-01 399/week @ 2024-06-08 442/week @ 2024-06-15 434/week @ 2024-06-22 103/week @ 2024-06-29

1,459 每月下载量
3 crates 中使用

MIT 许可证

38KB
620

简单XML序列化

这是一个用于将数据序列化为XML的Rust crate。可以手动构建XMLElement,或者使用simple_xml_serialize_macro crate来为结构体生成From实现。

示例用法

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());
}

使用 simple_xml_serialize_macro

使用此proc_macro crate可以通过为结构体添加#[xml_element("...")]注释来生成结构体到XMLElementFrom实现。单个字段使用sxs_type_attrsxs_type_textsxs_type_elementsxs_type_multi_element进行注释。未注释的字段将被忽略。

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", "  ")); 
}

无运行时依赖