Crates.io | easy-xml |
lib.rs | easy-xml |
version | 0.1.4 |
source | src |
created_at | 2021-10-07 21:34:27.677886 |
updated_at | 2021-10-17 16:14:05.890779 |
description | An XML library |
homepage | https://github.com/QAQtutu/easy-xml |
repository | https://github.com/QAQtutu/easy-xml |
max_upload_size | |
id | 462061 |
size | 32,547 |
基于xml-rs,将文本转换成节点树。通过宏实现将节点树转换成自定义结构体或其他类型。
|
分隔来匹配多种节点名称,例如#[easy_xml(rename = "Text|Img")]
。当使用|
且在序列化时,节点名称将由字段类型实现自己决定。属性或类型 | text | attribute | rename | prefix | flatten | root | namespace | skip | to_text | container |
---|---|---|---|---|---|---|---|---|---|---|
text | - | N | - | - | - | - | - | - | - | N |
attribute | N | - | Y | Y | - | - | - | - | - | N |
rename | - | Y | - | Y | - | Y | Y | - | Y | - |
prefix | - | Y | - | - | - | Y | Y | - | Y | - |
flatten | - | - | - | - | - | - | - | - | - | N |
root | - | - | Y | Y | - | - | Y | - | - | - |
namespace | - | - | Y | Y | - | - | Y | - | - | - |
skip | - | - | - | - | - | - | - | - | - | - |
to_text | - | - | Y | Y | - | - | - | - | - | N |
container | N | N | - | - | N | - | - | - | N | - |
Vec<String>
和Option<String>
合法,Option<Vec<String>>
是不合法的。依赖:
easy-xml = "0.1.4"
easy-xml-derive = "0.1.4"
使用:
#[macro_use]
extern crate easy_xml_derive;
#[derive(Debug, XmlDeserialize,XmlSerialize)]
#[easy_xml(root)]
struct Node {
#[easy_xml(text)]
text: String,
}
//反序列化
let node: Node = easy_xml::de::from_str("<Node>text</Node>").unwrap();
assert_eq!(node.text.as_str(), "text");
//序列化
let xml = easy_xml::se::to_string(&node).unwrap();
assert_eq!(
xml.as_str(),
r#"<?xml version="1.0" encoding="UTF-8"?><Node>text</Node>"#
);
//序列化
impl easy_xml::XmlSerialize for Node {
fn serialize(&self, element: &mut easy_xml::XmlElement)
where
Self: Sized,
{
todo!()
}
}
//反序列化
impl easy_xml::XmlDeserialize for Node {
fn deserialize(node: &easy_xml::XmlElement) -> Result<Self, easy_xml::de::Error>
where
Self: Sized,
{
todo!()
}
}
<Node>
<Child>123</Child>
<Child>456</Child>
</Node>
#[derive(XmlDeserialize,XmlSerialize)]
struct Node {
#[easy_xml(text)]
text: String, //123456
}
<Node attr="123456"></Node>
#[derive(XmlDeserialize,XmlSerialize)]
struct Node {
#[easy_xml(attribute)]
attr: String, //123456
}
<Node>
<Child>123456</Child>
</Node>
#[derive(XmlDeserialize,XmlSerialize)]
struct Node {
#[easy_xml(rename="Child")]
child: String, //123456
}
<easy:Node xmlns:easy="http://easy.org/">
<easy:Child>123456</easy:Child>
</easy:Node>
#[derive(XmlDeserialize,XmlSerialize)]
#[easy_xml(root,namespace = {"easy":"http://easy.org/"},prefix="easy")]
struct Node {
#[easy_xml(prefix="easy",rename="Child")]
child: String, //123456
}
<Node>
<Child>123456</Child>
</Node>
#[derive(XmlDeserialize,XmlSerialize)]
struct Node {
#[easy_xml(flatten)]
flatten:Flatten,
//等效于
// #[easy_xml(rename="Child")]
// child: String,
}
#[derive(XmlDeserialize,XmlSerialize)]
struct Flatten {
#[easy_xml(rename="Child")]
child: String, //123456
}
<Node></Node>
#[derive(XmlDeserialize,XmlSerialize)]
struct Node {
#[easy_xml(skip)]
child: String, //123456
}
<Node>
<Type>T1</Type>
<T2 />
</Node>
#[derive(XmlDeserialize,XmlSerialize)]
struct Node {
#[easy_xml(rename="Type",to_text)]
ty: Type, //T1
#[easy_xml(rename="T1|T2|T3")]
ty2: Type, //T2
}
enum Type{
T1,
T2,
T3,
}
<Node>
<Content>
<Child>123456</Child>
</Content>
</Node>
#[derive(XmlDeserialize,XmlSerialize)]
struct Node {
#[easy_xml(container,rename="Content")]
content: Vec<Child>,
}
#[derive(PartialEq, Debug, XmlSerialize, XmlDeserialize)]
struct Child {
#[easy_xml(text)]
val: String, //123456
}