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.

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

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择