douxu4610 2015-08-27 10:55
浏览 22
已采纳

解组嵌套的OPML文档

I am trying to unmarshal a simple nested opml document. The problem comes when I have nested opml and I am unable to decide on structure. I am putting the code below. Please let me know how can I parse the multi level of nesting of the Outline struct.

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)
    }
  • 写回答

1条回答 默认 最新

  • drqyxkzbs21968684 2015-08-27 11:42
    关注

    you need use pointer

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Windows Script Host 无法找到脚本文件"C:\ProgramData\Player800\Cotrl.vbs”
  • ¥15 matlab自定义损失函数
  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图