xml - 使用 serde-xml-rs 反序列化 XML 创建 Err(重复字段 `$value`)
问题描述
这是我尝试解析的 xml 模式的示例。只有几个领域是我感兴趣的。架构的根是PubmedArticleSet
包含 >= 0PubmedArticle
个条目的。我对这些条目中包含的字段感兴趣。我收到标题中的错误,但我在这里没有看到与该错误相关的问题。
<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
IE
<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 {
...
}
推荐阅读
- machine-learning - 学习率对神经网络的影响
- php - 试图从数据库中访问某些信息
- java - 如何从 mvnrepository 正确添加 Maven 依赖项
- javascript - Javascript:从两个具有相同标签名称的 XML 节点检索数据的可靠方法是什么?
- c# - 执行 Microsoft Graph API 的 POST 请求以将成员添加到 AD 组
- scala - SBT 不会运行凿子教程...似乎找不到合适的 scala 版本
- python - 比较两个数据框并根据查找表删除列
- c# - 使用 Linq 在 xml 中添加项目索引
- android - 尝试从另一个活动中的代码更改一个活动中的背景颜色(Kotlin/Android Studio)
- javascript - Firestore - 清理文本区域输入?