serde-xml-rs: 在序列化时出现LastElementNameNotAvailable错误。

huangapple go评论102阅读模式
英文:

serde-xml-rs: LastElementNameNotAvailable when serializing

问题

在我的项目中,我遇到了一个错误,其中我使用 serde-xml-rs 解析和序列化 XML。

Cargo.toml

[package]
name = "serde_xml_rs_serialization_error"
version = "0.1.0"
edition = "2021"

# 更多键及其定义,请参见 https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { version = "1.0.164", features = ["derive"] }
serde-xml-rs = "0.6.0"

src/main.rs

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
struct Types {
    #[serde(rename = "type")]
    types: Vec<Type>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
struct Type {
    name: String,
}

const XML: &str = r#"<types>
    <type name="first"/>
    <type name="second"/>
</types>"#;

fn main() {
    let types: Types = serde_xml_rs::from_str(XML).expect("无法解析 XML");
    dbg!(&types);
    match serde_xml_rs::to_string(&types) {
        Ok(xml) => {
            dbg!(xml);
        }
        Err(error) => {
            dbg!(error);
        }
    };
}

我期望得到一个序列化的 XML 字符串,但实际上它产生了一个错误:

cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.05s
     Running `target\debug\serde_xml_rs_serialization_error.exe`
[src\main.rs:21] &types = Types {
    types: [
        Type {
            name: "first",
        },
        Type {
            name: "second",
        },
    ],
}
[src\main.rs:27] error = Writer {
    source: LastElementNameNotAvailable,
}

我在这里做错了什么?
1: https://docs.rs/serde-xml-rs/latest/serde_xml_rs/

英文:

So I encountered this error in one of my projects where I parse and serialize XML using serde-xml-rs.

Cargo.toml

[package]
name = "serde_xml_rs_serialization_error"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { version = "1.0.164", features = ["derive"] }
serde-xml-rs = "0.6.0"

src/main.rs

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
struct Types {
    #[serde(rename = "type")]
    types: Vec<Type>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
struct Type {
    name: String,
}

const XML: &str = r#"<types>
    <type name="first"/>
    <type name="second"/>
</types>"#;

fn main() {
    let types: Types = serde_xml_rs::from_str(XML).expect("Cannot parse XML.");
    dbg!(&types);
    match serde_xml_rs::to_string(&types) {
        Ok(xml) => {
            dbg!(xml);
        }
        Err(error) => {
            dbg!(error);
        }
    };
}

I expected a serialized XML string, but instead it produces an error:

 cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.05s
     Running `target\debug\serde_xml_rs_serialization_error.exe`
[src\main.rs:21] &types = Types {
    types: [
        Type {
            name: "first",
        },
        Type {
            name: "second",
        },
    ],
}
[src\main.rs:27] error = Writer {
    source: LastElementNameNotAvailable,
}

What am I doing wrong here?

答案1

得分: 0

这是一个关于serde-xml-rs问题,让我浪费了半天的时间。

使用 quick-xml 替代 serde-xml-rs 解决了这个问题:

Cargo.toml

[package]
name = "serde_xml_rs_serialization_error"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { version = "1.0.164", features = ["derive"] }
quick-xml = { version = "0.28.2", features = ["serialize"] }

src/main.rs

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
struct Types {
    #[serde(rename = "type")]
    types: Vec<Type>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
struct Type {
    #[serde(rename = "@name")]
    name: String,
}

const XML: &str = r#"<types>
    <type name="first"/>
    <type name="second"/>
</types>"#;

fn main() {
    let types: Types = quick_xml::de::from_str(XML).expect("Cannot parse XML.");
    dbg!(&types);
    match quick_xml::se::to_string(&types) {
        Ok(xml) => {
            dbg!(xml);
        }
        Err(error) => {
            dbg!(error);
        }
    };
}
英文:

It's a bug in serde-xml-rs, that cost me half a day.

Using quick-xml instead of serde-xml-rs solved the issue:

Cargo.toml

[package]
name = &quot;serde_xml_rs_serialization_error&quot;
version = &quot;0.1.0&quot;
edition = &quot;2021&quot;

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { version = &quot;1.0.164&quot;, features = [&quot;derive&quot;] }
quick-xml = { version = &quot;0.28.2&quot;, features = [&quot;serialize&quot;] }

src/main.rs

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
struct Types {
    #[serde(rename = &quot;type&quot;)]
    types: Vec&lt;Type&gt;,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
struct Type {
    #[serde(rename = &quot;@name&quot;)]
    name: String,
}

const XML: &amp;str = r#&quot;&lt;types&gt;
    &lt;type name=&quot;first&quot;/&gt;
    &lt;type name=&quot;second&quot;/&gt;
&lt;/types&gt;&quot;#;

fn main() {
    let types: Types = quick_xml::de::from_str(XML).expect(&quot;Cannot parse XML.&quot;);
    dbg!(&amp;types);
    match quick_xml::se::to_string(&amp;types) {
        Ok(xml) =&gt; {
            dbg!(xml);
        }
        Err(error) =&gt; {
            dbg!(error);
        }
    };
}

huangapple
  • 本文由 发表于 2023年6月22日 03:37:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定