dotj6816 2017-02-23 18:10
浏览 50
已采纳

解组混合对象类型的xml数组

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

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
", reminder.Subject, reminder.Time)
  }

  for _, greeting := range reply.Messages.GreetingList {
    fmt.Printf("Greetings From: %s
", greeting.From)
  }

}

The output is

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.

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条回答 默认 最新

  • dongranding3909 2017-02-24 11:33
    关注

    You can handle unexpected XML elements using the , any and , innerxml tags.

    code changes are (run full example at go playground)

      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'
    ", unexpected.XMLName, unexpected.Content)
      }
    

    output

    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>'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器