这是我尝试解析的 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")?
}
}