duangou1551 2018-02-14 20:14
浏览 20
已采纳

解组具有可变子类型的XML

I have XML that looks like this:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<smses count="500">
  <sms id="1" text="hi" sms_specific_field="blah" />
  <sms id="2" text="what's up?" sms_specific_field="blah" />
  <mms id="3" text="null" text_only="0">
    <parts>
      <part seq="-1" content="image/jpeg" text="null" data="base64_data_here==" />
      <part seq="0" content="text/plain" text="Check it out!" />
    </parts>
  </mms>
  <sms id="4" text="what's up?" sms_specific_field="blah" />
</smses>

The sms and mms children of smses can occur in any order. I'd like to unmarshal this data into native Go structs. I thought I could use a slice of interfaces for those like:

Messages []interface{} `xml:",any"`  // not sure if this is correct

But then how do I get that into marshaled structs for each type?

I was thinking of approaching it like so but not sure if this works to cover both and don't want to spend all the time writing this for structs that will have a ton more attributes if this whole approach won't work anyways:

type Messages struct {
    XMLName       xml.Name   `xml:"smses"`
    Count         string     `xml:"count,attr"`
    MessageList   []Message  `xml:",any"` // <-- will this work?
}

type Message struct {
    SMS           SMS        `xml:"sms"`
    MMS           MMS        `xml:"mms"`
    ID            string     `xml"id,attr"`
    Text          string     `xml:"text,attr"`
}

type SMS struct {
    XMLName       xml.Name   `xml:"sms"`
    SMSSpecField  string     `xml:"sms_specific_field,attr"`
}

type MMS struct {
    XMLName       xml.Name   `xml:"mms"`
    TextOnly      string     `xml:"text_only,attr"`
    Parts         []Part     `xml:"parts"`
}

... And so on - but I don't know if this approach works / makes sense

How do I design my structs in order to unmarshal this XML including attributes and child items?

  • 写回答

1条回答 默认 最新

  • duanlie3187 2018-02-14 20:34
    关注

    One possible approach would be something like this:

    type Messages struct {
        XMLName xml.Name `xml:"smses"`
        Count   string   `xml:"count,attr"`
        MMS     []MMS    `xml:"mms"`
        SMS     []SMS    `xml:"sms"`
    }
    

    This will unmarshal all child mms elements into MMS and all child sms elements into SMS. You can then iterate these for further processing as necessary, e.g. embedding them into Message objects and putting those into a combined slice. This will lose the original XML source order, however.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端