gpt4 book ai didi

xml - 使用 serde-xml-rs 反序列化 XML 会创建错误(重复字段 `$value`)

转载 作者:行者123 更新时间:2023-11-29 08:18:27 25 4
gpt4 key购买 nike

This是我试图解析的 xml 模式的示例。我只对几个领域感兴趣。架构的根是 PubmedArticleSet,其中包含 >= 0 个 PubmedArticle 条目。我对这些条目中包含的字段感兴趣。我在标题中收到错误,但我没有看到与该错误相关的问题 here .

<PubmedArticle>
<MedlineCitation>
<PMID>xxxxxxxx</PMID>
<date tags i don't care about/>
<other date tags i don't care about/>
<Article>
<tags I don't care about/>
<children I don't care about>
<other tags I don't care about/>
<children I don't care about>
<AuthorList>
<Author>
<LastName>xxxx</LastName>
<FirstName>i don't care about this</FirstName>
<Initials>xx</Initials>
<AffiliationInfo>
<Affiliation>String of text</Affiliation>
</AffiliationInfo>
</Author>
<Author>same as above</Author>
</AuthorList>
<Lots of stuff I don't care about/>
</Article>
<More stuff I don't care about/>
</MedlineCitation>
<Final stuff I don't care about/>
</PubmedArticle>

我已经设置了以下结构:

#[derive(Serialize, Deserialize, Debug)]
struct PubmedArticleSet {
#[serde(rename="$value")]
pub articleset: Vec<PubmedArticle>
}

#[derive(Serialize, Deserialize, Debug)]
struct PubmedArticle {
#[serde(rename="$value")]
pub medlinecitation: MedlineCitation,
}

#[derive(Serialize, Deserialize, Debug)]
struct MedlineCitation {
#[serde(rename="$value")]
pub pmid: PMID,
pub article: Article
}

#[derive(Serialize, Deserialize, Debug)]
struct PMID {
#[serde(rename="$value")]
pub id: String
}

#[derive(Serialize, Deserialize, Debug)]
struct Article {
pub authorlist: AuthorList,
pub publicationtypelist: Vec<PublicationType>
}

#[derive(Serialize, Deserialize, Debug)]
struct PublicationType {
#[serde(rename="$value")]
pub publicationtype: String
}

#[derive(Serialize, Deserialize, Debug)]
struct AuthorList {
#[serde(rename="$value")]
pub authorlist: Vec<Author>,
}

#[derive(Serialize, Deserialize, Debug)]
struct Author {
#[serde(rename="$value")]
pub author: (LastName, Initials),
pub affiliation: Affiliation
}

#[derive(Serialize, Deserialize, Debug)]
struct LastName {
#[serde(rename="$value")]
pub lastname: String
}

#[derive(Serialize, Deserialize, Debug)]
struct Initials {
#[serde(rename="$value")]
pub initials: String
}

#[derive(Serialize, Deserialize, Debug)]
struct Affiliation {
#[serde(rename="$value")]
pub affiliation: String

然后我尝试使用以下函数进行解析:

fn deser_article_records(result: &String) -> Result<PubmedArticleSet, Box<Error>> {
if let Some(start) = result.find("<PubmedArticleSet>") {
let records = serde_xml_rs::deserialize(result[start..].as_bytes())?;
Ok(records)
} else {
Err("no articleset found")?
}
}

最佳答案

我的猜测是您有 XML 元素和介于两者之间的其他东西。您没有在上面的代码片段中显示示例,但是例如:

<Author ...>
<SomethingElse ...>
<Author>

serde_xml_rs 似乎希望所有标签根据此评论相邻:https://github.com/RReverser/serde-xml-rs/issues/55

<Author...>
<Author...>
<SomethingElse ...>

如果您的 XML 不是这样,您可以使用 Enum 来解决这个问题。

例如

#[derive(Deserialize, Debug)]
struct Article {
details: Vec<Detail>
}

#[derive(Deserialize, Debug)]
enum Detail {
Author(Author),
SomethingElse(SomethingElse)
}

#[derive(Debug, Deserialize)]
struct Author {
...
}

#[derive(Debug, Deserialize)]
struct SomethingElse {
...
}

关于xml - 使用 serde-xml-rs 反序列化 XML 会创建错误(重复字段 `$value`),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50083753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com