【问题标题】:Unmarshal a nested OPML Document解组嵌套的 OPML 文档
【发布时间】:2015-08-27 10:55:14
【问题描述】:

我正在尝试解组一个简单的嵌套 opml 文档。当我嵌套了 opml 并且我无法决定结构时,问题就来了。我把代码放在下面。请告诉我如何解析 Outline 结构的多级嵌套。

http://play.golang.org/p/FobiL1JDdb

    package main

    import (
        "encoding/xml"
        "fmt"
    )

    var response = `<opml version='1.0'>
     <head>
      <title>More Cases</title>
      <expansionState>1,6,26</expansionState>
     </head>
     <body>
      <outline text='Testing' _note='indeterminate'>
       <outline text='Weekly' _status='indeterminate'>
       </outline>
       </outline>
     </body>
    </opml>`

    type Opml struct {
        XMLName xml.Name
        Version string `xml:"version,attr"`
        Head    Head   `xml:"head"`
        Body    Body   `xml:"body"`
    }

    type Head struct {
        Title          string `xml:"title"`
        ExpansionState string `xml:"expansionState"`
    }

    type Body struct {
        Outline Outline `xml:"outline"`
    }
    type Outline struct {
        Text string `xml:"text,attr"`
        Note string `xml:"_note,attr"`
    }

    func main() {

        fmt.Println("UnMrashal XML")
        opml := &Opml{}
        xml.Unmarshal([]byte(response), opml)
        fmt.Println(opml)
    }

【问题讨论】:

    标签: go opml


    【解决方案1】:

    你需要使用指针

    type Outline struct {
        Text string `xml:"text,attr"`
        Note string `xml:"_note,attr"`
        Outline *Outline `xml:"outline"`
    }
    

    http://play.golang.org/p/f1UqEkJq4S

    【讨论】:

      猜你喜欢
      • 2018-07-24
      • 2021-05-01
      • 1970-01-01
      • 2018-09-19
      • 2015-07-13
      • 2018-03-09
      • 2020-11-28
      相关资源
      最近更新 更多