解析混合对象类型的 XML 数组

huangapple go评论81阅读模式
英文:

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 (
&quot;encoding/xml&quot;
&quot;fmt&quot;
)
func main() {
data := `&lt;Envelope&gt;
&lt;Body&gt;
&lt;Response&gt;
&lt;Messages&gt;
&lt;Greeting&gt;
&lt;From&gt;Fred&lt;/From&gt;
&lt;/Greeting&gt;
&lt;Reminder&gt;
&lt;Time&gt;12&lt;/Time&gt;
&lt;Subject&gt;Lunch at Joe&#39;s&lt;/Subject&gt;
&lt;/Reminder&gt;
&lt;NewThing&gt;Report me!&lt;/NewThing&gt;
&lt;Reminder&gt;
&lt;Time&gt;6&lt;/Time&gt;
&lt;Subject&gt;Catch the train&lt;/Subject&gt;
&lt;/Reminder&gt;
&lt;Greeting&gt;
&lt;From&gt;Mary&lt;/From&gt;
&lt;Extra&gt;Hi&lt;/Extra&gt;
&lt;/Greeting&gt;
&lt;/Messages&gt;
&lt;MessageCount&gt;3&lt;/MessageCount&gt;
&lt;/Response&gt;
&lt;/Body&gt;
&lt;/Envelope&gt;`
type Greeting struct {
From string 
}
type Reminder struct {
Time int
Subject string
}
type TopLevel struct {
Messages struct {
GreetingList []Greeting `xml:&quot;Greeting&quot;`
ReminderList []Reminder `xml:&quot;Reminder&quot;`
} `xml:&quot;Body&gt;Response&gt;Messages&quot;`
}
var reply TopLevel
err := xml.Unmarshal([]byte(data), &amp;reply) 
if err != nil {
fmt.Println(err)
return
}
for _, reminder := range reply.Messages.ReminderList {
fmt.Printf(&quot;Reminder: &#39;%s&#39; at %d\n&quot;, reminder.Subject, reminder.Time)
}
for _, greeting := range reply.Messages.GreetingList {
fmt.Printf(&quot;Greetings From: %s\n&quot;, greeting.From)
}
}

The output is

<!-- language: lang-none -->

Reminder: &#39;Lunch at Joe&#39;s&#39; at 12
Reminder: &#39;Catch the train&#39; at 6
Greetings From: Fred
Greetings From: Mary

I would also like to be able to find unexpected types of message that are neither &lt;Greeting&gt; nor &lt;Reminder&gt;, such as the &lt;NewThing&gt; 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:&quot;,innerxml&quot;`
}
type TopLevel struct {
Messages struct {
GreetingList   []Greeting `xml:&quot;Greeting&quot;`
ReminderList   []Reminder `xml:&quot;Reminder&quot;`
UnexpectedList []Any      `xml:&quot;,any&quot;`
} `xml:&quot;Body&gt;Response&gt;Messages&quot;`
}
...
for _, unexpected := range reply.Messages.UnexpectedList {
fmt.Printf(&quot;Unexpected: %s containing &#39;%s&#39;\n&quot;, unexpected.XMLName, unexpected.Content)
}

output

<!-- language: lang-none -->

Reminder: &#39;Lunch at Joe&#39;s&#39; at 12
Reminder: &#39;Catch the train&#39; at 6
Greetings From: Fred
Greetings From: Mary
Unexpected: { NewThing} containing &#39;Report me!&#39;
Unexpected: { NewerThing} containing &#39;&lt;Fruit&gt;Apple&lt;/Fruit&gt;&lt;Sales&gt;42&lt;/Sales&gt;&#39;

huangapple
  • 本文由 发表于 2017年2月24日 02:10:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/42423151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定