英文:
unmarshalling xml arrays of mixed object types
问题
我正在处理接收到的包含无序消息列表的 XML,其中每个消息可以是几种不同类型之一。顺序不重要。
我已经找到了一种实现我想要的方式(经过了很多努力,这是我学习 Go 的第三天),但是在如何强大地处理意外的消息类型方面遇到了困难。
以下是一些代码:
package main
import (
"encoding/xml"
"fmt"
)
func main() {
data := `<Envelope>
<Body>
<Response>
<Messages>
<Greeting>
<From>Fred</From>
</Greeting>
<Reminder>
<Time>12</Time>
<Subject>Lunch at Joe's</Subject>
</Reminder>
<NewThing>Report me!</NewThing>
<Reminder>
<Time>6</Time>
<Subject>Catch the train</Subject>
</Reminder>
<Greeting>
<From>Mary</From>
<Extra>Hi</Extra>
</Greeting>
</Messages>
<MessageCount>3</MessageCount>
</Response>
</Body>
</Envelope>`
type Greeting struct {
From string
}
type Reminder struct {
Time int
Subject string
}
type TopLevel struct {
Messages struct {
GreetingList []Greeting `xml:"Greeting"`
ReminderList []Reminder `xml:"Reminder"`
} `xml:"Body>Response>Messages"`
}
var reply TopLevel
err := xml.Unmarshal([]byte(data), &reply)
if err != nil {
fmt.Println(err)
return
}
for _, reminder := range reply.Messages.ReminderList {
fmt.Printf("Reminder: '%s' at %d\n", reminder.Subject, reminder.Time)
}
for _, greeting := range reply.Messages.GreetingList {
fmt.Printf("Greetings From: %s\n", greeting.From)
}
}
输出结果为:
Reminder: 'Lunch at Joe's' at 12
Reminder: 'Catch the train' at 6
Greetings From: Fred
Greetings From: Mary
我还希望能够找到既不是 <Greeting>
也不是 <Reminder>
的意外消息类型,例如 <NewThing>
,而不需要事先了解任何关于新事物的信息。
Warning: Unexpected message type: NewThing.
我应该考虑一种使用通用对象的单一列表的方式吗?或者以某种方式使用 XMLname + innerxml 字符串的结构体?不确定如何处理这种不同类型的单一列表。
有什么线索吗?
英文:
I am processing received XML which contains an unordered list of messages where each message can be of one of several distinct types. Order isn't significant.
I have worked out a way to do what I want (after much struggle, this is day 3 of learning go) but am hung up on how to robustly deal with unexpected message types
Here's some code
<!-- language: go -->
package main
import (
"encoding/xml"
"fmt"
)
func main() {
data := `<Envelope>
<Body>
<Response>
<Messages>
<Greeting>
<From>Fred</From>
</Greeting>
<Reminder>
<Time>12</Time>
<Subject>Lunch at Joe's</Subject>
</Reminder>
<NewThing>Report me!</NewThing>
<Reminder>
<Time>6</Time>
<Subject>Catch the train</Subject>
</Reminder>
<Greeting>
<From>Mary</From>
<Extra>Hi</Extra>
</Greeting>
</Messages>
<MessageCount>3</MessageCount>
</Response>
</Body>
</Envelope>`
type Greeting struct {
From string
}
type Reminder struct {
Time int
Subject string
}
type TopLevel struct {
Messages struct {
GreetingList []Greeting `xml:"Greeting"`
ReminderList []Reminder `xml:"Reminder"`
} `xml:"Body>Response>Messages"`
}
var reply TopLevel
err := xml.Unmarshal([]byte(data), &reply)
if err != nil {
fmt.Println(err)
return
}
for _, reminder := range reply.Messages.ReminderList {
fmt.Printf("Reminder: '%s' at %d\n", reminder.Subject, reminder.Time)
}
for _, greeting := range reply.Messages.GreetingList {
fmt.Printf("Greetings From: %s\n", greeting.From)
}
}
The output is
<!-- language: lang-none -->
Reminder: 'Lunch at Joe's' at 12
Reminder: 'Catch the train' at 6
Greetings From: Fred
Greetings From: Mary
I would also like to be able to find unexpected types of message that are neither <Greeting>
nor <Reminder>
, such as the <NewThing>
without knowing anything about new things (etc) in advance.
<!-- language: lang-none -->
Warning: Unexpected message type: NewThing.
Should I be thinking of some way to have a single list of generic objects? Or using structs of XMLname + innerxml strings somehow? Not sure how to process a single list of such dissimilar types.
Clues?
答案1
得分: 1
你可以使用, any
和, innerxml
标签来处理意外的XML元素。
代码更改如下(在Go Playground上运行完整示例):
<!-- language: go -->
type Any struct {
XMLName xml.Name
Content string `xml:",innerxml"`
}
type TopLevel struct {
Messages struct {
GreetingList []Greeting `xml:"Greeting"`
ReminderList []Reminder `xml:"Reminder"`
UnexpectedList []Any `xml:",any"`
} `xml:"Body>Response>Messages"`
}
...
for _, unexpected := range reply.Messages.UnexpectedList {
fmt.Printf("Unexpected: %s containing '%s'\n", unexpected.XMLName, unexpected.Content)
}
输出结果:
<!-- language: lang-none -->
Reminder: 'Lunch at Joe's' at 12
Reminder: 'Catch the train' at 6
Greetings From: Fred
Greetings From: Mary
Unexpected: { NewThing} containing 'Report me!'
Unexpected: { NewerThing} containing '<Fruit>Apple</Fruit><Sales>42</Sales>'
英文:
You can handle unexpected XML elements using the , any
and , innerxml
tags.
code changes are (run full example at go playground)
<!-- language: go -->
type Any struct {
XMLName xml.Name
Content string `xml:",innerxml"`
}
type TopLevel struct {
Messages struct {
GreetingList []Greeting `xml:"Greeting"`
ReminderList []Reminder `xml:"Reminder"`
UnexpectedList []Any `xml:",any"`
} `xml:"Body>Response>Messages"`
}
...
for _, unexpected := range reply.Messages.UnexpectedList {
fmt.Printf("Unexpected: %s containing '%s'\n", unexpected.XMLName, unexpected.Content)
}
output
<!-- language: lang-none -->
Reminder: 'Lunch at Joe's' at 12
Reminder: 'Catch the train' at 6
Greetings From: Fred
Greetings From: Mary
Unexpected: { NewThing} containing 'Report me!'
Unexpected: { NewerThing} containing '<Fruit>Apple</Fruit><Sales>42</Sales>'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论