使用变量 child 类型解组 XML
Unmarshal XML with variable child types
我有 XML 看起来像这样:
<?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>
sms
和 mms
children smses
可以任意顺序出现。我想将这些数据解组为本机 Go 结构。我想我可以为这样的人使用一些接口:
Messages []interface{} `xml:",any"` // not sure if this is correct
但是我如何将其放入每种类型的封送结构中?
我正在考虑像这样接近它,但不确定这是否适用于涵盖两者,并且不想花所有时间为将具有更多属性的结构编写这个,如果整个方法不会无论如何工作:
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
如何设计我的结构以解组这个 XML 包括属性和 child 项?
一种可能的方法是这样的:
type Messages struct {
XMLName xml.Name `xml:"smses"`
Count string `xml:"count,attr"`
MMS []MMS `xml:"mms"`
SMS []SMS `xml:"sms"`
}
这会将所有子 mms
元素解组为 MMS
,将所有子 sms
元素解组为 SMS
。然后,您可以根据需要迭代这些以进行进一步处理,例如将它们嵌入 Message
个对象并将它们放入组合切片中。但是,这将丢失原始的 XML 源顺序。
我有 XML 看起来像这样:
<?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>
sms
和 mms
children smses
可以任意顺序出现。我想将这些数据解组为本机 Go 结构。我想我可以为这样的人使用一些接口:
Messages []interface{} `xml:",any"` // not sure if this is correct
但是我如何将其放入每种类型的封送结构中?
我正在考虑像这样接近它,但不确定这是否适用于涵盖两者,并且不想花所有时间为将具有更多属性的结构编写这个,如果整个方法不会无论如何工作:
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
如何设计我的结构以解组这个 XML 包括属性和 child 项?
一种可能的方法是这样的:
type Messages struct {
XMLName xml.Name `xml:"smses"`
Count string `xml:"count,attr"`
MMS []MMS `xml:"mms"`
SMS []SMS `xml:"sms"`
}
这会将所有子 mms
元素解组为 MMS
,将所有子 sms
元素解组为 SMS
。然后,您可以根据需要迭代这些以进行进一步处理,例如将它们嵌入 Message
个对象并将它们放入组合切片中。但是,这将丢失原始的 XML 源顺序。