首页 > 解决方案 > 使用 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")?
    }
}

标签: xmlrustserde

解决方案


我的猜测是你有 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 {
    ...
}

推荐阅读